From 7de9dd7f2ffdacfbbb0a4bdbbe560351e4bbdf49 Mon Sep 17 00:00:00 2001 From: twangboy Date: Wed, 20 Dec 2017 14:57:02 -0700 Subject: [PATCH] Fix a stack trace in grains.core On Windows machines that do not have fiber channel cards, core.py was throwing a stack trace everytime it tried to populate the fiber channel port for grains --- salt/grains/core.py | 2 +- salt/modules/cmdmod.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/salt/grains/core.py b/salt/grains/core.py index 81962d8c970..befd511161b 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -2550,7 +2550,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,