using shell='powershell'

This commit is contained in:
ranl 2013-10-06 09:58:59 +03:00
parent 1caa086a2f
commit 57267d4716
2 changed files with 13 additions and 13 deletions

View File

@ -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

View File

@ -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)