diff options
author | James Cammarata <jimi@sngx.net> | 2015-08-23 21:09:16 -0400 |
---|---|---|
committer | James Cammarata <jimi@sngx.net> | 2015-08-23 21:09:16 -0400 |
commit | db6550377890c9be5c1aab62681c4f4081aab719 (patch) | |
tree | b1eb0d41941e3225c66e461b2b31d8cde6934225 /lib | |
parent | 3aedc0bca9f1233949082874d93d20df9e7c3565 (diff) | |
download | ansible-revert-12047-powershell_common_cleanup.tar.gz |
Revert "Add PowerShell exception handling and turn on strict mode."revert-12047-powershell_common_cleanup
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ansible/module_utils/powershell.ps1 | 45 | ||||
-rw-r--r-- | lib/ansible/plugins/connections/winrm.py | 2 | ||||
-rw-r--r-- | lib/ansible/plugins/shell/powershell.py | 45 |
3 files changed, 22 insertions, 70 deletions
diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index 5756d360f1..ee65916216 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -26,8 +26,6 @@ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -Set-StrictMode -Version Latest - # Ansible v2 will insert the module arguments below as a string containing # JSON; assign them to an environment variable and redefine $args so existing # modules will continue to work. @@ -49,14 +47,7 @@ Function Set-Attr($obj, $name, $value) $obj = New-Object psobject } - Try - { - $obj.$name = $value - } - Catch - { - $obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value - } + $obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value } # Helper function to convert a powershell object to JSON to echo it, exiting @@ -87,7 +78,7 @@ Function Fail-Json($obj, $message = $null) $obj = New-Object psobject } # If the first args is undefined or not an object, make it an object - ElseIf (-not $obj -or -not $obj.GetType -or $obj.GetType().Name -ne "PSCustomObject") + ElseIf (-not $obj.GetType -or $obj.GetType().Name -ne "PSCustomObject") { $obj = New-Object psobject } @@ -103,32 +94,24 @@ Function Fail-Json($obj, $message = $null) # slightly more pythonic # Example: $attr = Get-Attr $response "code" -default "1" #Note that if you use the failifempty option, you do need to specify resultobject as well. -Function Get-Attr($obj, $name, $default = $null, $resultobj, $failifempty=$false, $emptyattributefailmessage) +Function Get-Attr($obj, $name, $default = $null,$resultobj, $failifempty=$false, $emptyattributefailmessage) { - # Check if the provided Member $name exists in $obj and return it or the default. - Try + # Check if the provided Member $name exists in $obj and return it or the + # default + If ($obj.$name.GetType) { - If (-not $obj.$name.GetType) - { - throw - } $obj.$name } - Catch + Elseif($failifempty -eq $false) + { + $default + } + else { - If ($failifempty -eq $false) - { - $default - } - Else - { - If (!$emptyattributefailmessage) - { - $emptyattributefailmessage = "Missing required argument: $name" - } - Fail-Json -obj $resultobj -message $emptyattributefailmessage - } + if (!$emptyattributefailmessage) {$emptyattributefailmessage = "Missing required argument: $name"} + Fail-Json -obj $resultobj -message $emptyattributefailmessage } + return } # Helper filter/pipeline function to convert a value to boolean following current diff --git a/lib/ansible/plugins/connections/winrm.py b/lib/ansible/plugins/connections/winrm.py index 28bac3285a..0e19b93ac2 100644 --- a/lib/ansible/plugins/connections/winrm.py +++ b/lib/ansible/plugins/connections/winrm.py @@ -167,7 +167,7 @@ class Connection(ConnectionBase): elif '-EncodedCommand' not in cmd_parts: script = ' '.join(cmd_parts) if script: - cmd_parts = self._shell._encode_script(script, as_list=True, strict_mode=False) + cmd_parts = self._shell._encode_script(script, as_list=True) if '-EncodedCommand' in cmd_parts: encoded_cmd = cmd_parts[cmd_parts.index('-EncodedCommand') + 1] decoded_cmd = to_unicode(base64.b64decode(encoded_cmd)) diff --git a/lib/ansible/plugins/shell/powershell.py b/lib/ansible/plugins/shell/powershell.py index aba3183e76..0e16d34e16 100644 --- a/lib/ansible/plugins/shell/powershell.py +++ b/lib/ansible/plugins/shell/powershell.py @@ -112,41 +112,12 @@ class ShellModule(object): cmd_parts.insert(0, '&') elif shebang and shebang.startswith('#!'): cmd_parts.insert(0, shebang[2:]) - script = ''' - Try - { - %s - } - Catch - { - $_obj = @{ failed = $true } - If ($_.Exception.GetType) - { - $_obj.Add('msg', $_.Exception.Message) - } - Else - { - $_obj.Add('msg', $_.ToString()) - } - If ($_.InvocationInfo.PositionMessage) - { - $_obj.Add('exception', $_.InvocationInfo.PositionMessage) - } - ElseIf ($_.ScriptStackTrace) - { - $_obj.Add('exception', $_.ScriptStackTrace) - } - Try - { - $_obj.Add('error_record', ($_ | ConvertTo-Json | ConvertFrom-Json)) - } - Catch - { - } - Echo $_obj | ConvertTo-Json -Compress -Depth 99 - Exit 1 - } - ''' % (' '.join(cmd_parts)) + catch = ''' + $_obj = @{ failed = $true; $msg = $_ } + echo $_obj | ConvertTo-Json -Compress -Depth 99 + Exit 1 + ''' + script = 'Try { %s }\nCatch { %s }' % (' '.join(cmd_parts), 'throw') if rm_tmp: rm_tmp = self._escape(self._unquote(rm_tmp)) rm_cmd = 'Remove-Item "%s" -Force -Recurse -ErrorAction SilentlyContinue' % rm_tmp @@ -178,11 +149,9 @@ class ShellModule(object): replace = lambda m: substs[m.lastindex - 1] return re.sub(pattern, replace, value) - def _encode_script(self, script, as_list=False, strict_mode=True): + def _encode_script(self, script, as_list=False): '''Convert a PowerShell script to a single base64-encoded command.''' script = to_unicode(script) - if strict_mode: - script = u'Set-StrictMode -Version Latest\r\n%s' % script script = '\n'.join([x.strip() for x in script.splitlines() if x.strip()]) encoded_script = base64.b64encode(script.encode('utf-16-le')) cmd_parts = _common_args + ['-EncodedCommand', encoded_script] |