From 57267d4716ed0586844809474734b44a8d7c542f Mon Sep 17 00:00:00 2001 From: ranl Date: Sun, 6 Oct 2013 09:58:59 +0300 Subject: [PATCH] using shell='powershell' --- salt/modules/rdp.py | 20 ++++++++++---------- salt/modules/win_servermanager.py | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/salt/modules/rdp.py b/salt/modules/rdp.py index 428d6dca2c..52b54be39b 100644 --- a/salt/modules/rdp.py +++ b/salt/modules/rdp.py @@ -9,7 +9,6 @@ import re # Import salt libs import salt.utils -POWERSHELL = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe' def __virtual__(): @@ -31,6 +30,13 @@ def _parse_return_code_powershell(string): else: return int(regex.group(1)) +def _psrdp(cmd) + ''' + Create a Win32_TerminalServiceSetting WMI Object as $RDP and execute the command cmd + returns the STDOUT of the command + ''' + rdp = '$RDP = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace root\\CIMV2\\TerminalServices -Computer . -Authentication 6 -ErrorAction Stop' + return __salt__['cmd.run']('{0} ; {1}'.format(rdp, cmd), shell='powershell') def enable(): ''' @@ -43,9 +49,7 @@ def enable(): salt '*' rdp.enable ''' - cmd = '-InputFormat None -Command "& { $RDP = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace root\\CIMV2\\TerminalServices -Computer . -Authentication 6 -ErrorAction Stop ; $RDP.SetAllowTsConnections(1,1) }"' - cmd = '{0} {1}'.format(POWERSHELL, cmd) - return _parse_return_code_powershell(__salt__['cmd.run'](cmd)) == 0 + return _parse_return_code_powershell(_psrdp('$RDP.SetAllowTsConnections(1,1)')) == 0 def disable(): @@ -59,9 +63,7 @@ def disable(): salt '*' rdp.disable ''' - cmd = '-InputFormat None -Command "& { $RDP = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace root\\CIMV2\\TerminalServices -Computer . -Authentication 6 -ErrorAction Stop ; $RDP.SetAllowTsConnections(0,1) }"' - cmd = '{0} {1}'.format(POWERSHELL, cmd) - return _parse_return_code_powershell(__salt__['cmd.run'](cmd)) == 0 + return _parse_return_code_powershell(_psrdp('$RDP.SetAllowTsConnections(0,1)')) == 0 def status(): @@ -75,7 +77,5 @@ def status(): salt '*' rdp.status ''' - cmd = '-InputFormat None -Command "& { $RDP = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace root\\CIMV2\\TerminalServices -Computer . -Authentication 6 -ErrorAction Stop ; echo $RDP.AllowTSConnections }"' - cmd = '{0} {1}'.format(POWERSHELL, cmd) - out = int(__salt__['cmd.run'](cmd).strip()) + out = int(_psrdp('echo $RDP.AllowTSConnections').strip()) return out != 0 diff --git a/salt/modules/win_servermanager.py b/salt/modules/win_servermanager.py index 7eae5257e8..245fe27b23 100644 --- a/salt/modules/win_servermanager.py +++ b/salt/modules/win_servermanager.py @@ -20,7 +20,7 @@ def _srvmgr(func): ''' Execute a function from the ServerManager PS module and return the STDOUT ''' - return __salt__['cmd.run']('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell -InputFormat None -Command "& {{ Import-Module ServerManager ; {0} }}"'.format(func)) + return __salt__['cmd.run']('Import-Module ServerManager ; {0}'.format(func), shell='powershell') def _parse_powershell_list(lst): @@ -96,7 +96,7 @@ def install(feature, recurse=False): sub = '' if recurse: sub = '-IncludeAllSubFeature' - out = _srvmgr('Add-WindowsFeature -Name {0} {1} -erroraction silentlycontinue | format-list'.format(feature, sub)) + out = _srvmgr('"Add-WindowsFeature -Name {0} {1} -erroraction silentlycontinue | format-list"'.format(feature, sub)) return _parse_powershell_list(out) @@ -117,5 +117,5 @@ def remove(feature): salt -t 600 '*' win_servermanager.remove Telnet-Client ''' - out = _srvmgr('Remove-WindowsFeature -Name {0} -erroraction silentlycontinue | format-list'.format(feature)) + out = _srvmgr('"Remove-WindowsFeature -Name {0} -erroraction silentlycontinue | format-list"'.format(feature)) return _parse_powershell_list(out)