diff --git a/salt/grains/core.py b/salt/grains/core.py index e1f0390205d..22fc1b6d6f7 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -2552,7 +2552,7 @@ def _windows_wwns(): ''' Return Fibre Channel port WWNs from a Windows host. ''' - ps_cmd = r'Get-WmiObject -class MSFC_FibrePortHBAAttributes -namespace "root\WMI" | Select -Expandproperty Attributes | %{($_.PortWWN | % {"{0:x2}" -f $_}) -join ""}' + ps_cmd = r'Get-WmiObject -ErrorAction Stop -class MSFC_FibrePortHBAAttributes -namespace "root\WMI" | Select -Expandproperty Attributes | %{($_.PortWWN | % {"{0:x2}" -f $_}) -join ""}' ret = [] diff --git a/salt/modules/cmdmod.py b/salt/modules/cmdmod.py index bc4678346c8..bcf7587940d 100644 --- a/salt/modules/cmdmod.py +++ b/salt/modules/cmdmod.py @@ -3241,6 +3241,13 @@ def powershell(cmd, else: encoded_cmd = False + # Put the whole command inside a try / catch block + # Some errors in PowerShell are not "Terminating Errors" and will not be + # 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}' + # Retrieve the response, while overriding shell with 'powershell' response = run(cmd, cwd=cwd,