From 16f7d44aae7f94c5438c95f3a00158c0aa36a424 Mon Sep 17 00:00:00 2001 From: twangboy Date: Mon, 12 Mar 2018 17:50:03 -0600 Subject: [PATCH] Remove ConvertTo-JSON on Win 7 / 2008R2 and lower --- salt/modules/cmdmod.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/salt/modules/cmdmod.py b/salt/modules/cmdmod.py index 714e94046e..7d5e9e9d46 100644 --- a/salt/modules/cmdmod.py +++ b/salt/modules/cmdmod.py @@ -12,6 +12,7 @@ import functools import glob import logging import os +import platform import shutil import subprocess import sys @@ -3184,9 +3185,14 @@ def powershell(cmd, python_shell = True # Append PowerShell Object formatting - cmd += ' | ConvertTo-JSON' - if depth is not None: - cmd += ' -Depth {0}'.format(depth) + # ConvertTo-JSON is only available on Versions of Windows greater than + # `7.1.7600`. We have to use `platform.version` instead of `__grains__` here + # because this function is called by `salt/grains/core.py` before + # `__grains__` is populated + if salt.utils.versions.version_cmp(platform.version(), '7.1.7600') == 1: + cmd += ' | ConvertTo-JSON' + if depth is not None: + cmd += ' -Depth {0}'.format(depth) if encode_cmd: # Convert the cmd to UTF-16LE without a BOM and base64 encode. @@ -3203,7 +3209,7 @@ def powershell(cmd, # caught in a try/catch block. For example, the `Get-WmiObject` command will # often return a "Non Terminating Error". To fix this, make sure # `-ErrorAction Stop` is set in the powershell command - cmd = 'try {' + cmd + '} catch { "{}" | ConvertTo-JSON}' + cmd = 'try {' + cmd + '} catch { "{}" }' # Retrieve the response, while overriding shell with 'powershell' response = run(cmd,