有非常有用的脚本sudo.ps1
:
$w=""; foreach($a in $args[1..($args.length-1)] ){ $w += " " + $a }; $w
Start-Process $args[0] -ArgumentList $w -Verb RunAs -Wait
但它无法处理复杂的命令
./sudo.ps1 schtasks /create /F /TN "$vpn_name Connection Update" /TR "Powershell.exe -noexit -command D:\vpn-route.ps1" /SC ONEVENT /EC Application /MO "*[System[(Level=4 or Level=0) and (EventID=20225)]] and *[EventData[Data='$vpn_name']]" /RL HIGHEST
问题在于混淆的引号。在sudo.ps1
引号中打开:
/create /F /TN VPN-Kosmos6 Connection Update /TR Powershell.exe -noexit -command D:\vpn-route.ps1 /SC ONEVENT /EC Application /MO *[System[(Level=4 or Level=0) and (EventID=20225)]] and *[EventData[Data='VPN-Kosmos6']] /RL HIGHEST
执行命令没有错误,但没有工作。如何解决?
答案 0 :(得分:0)
如果arg包含空格,请通过"""
添加引号(感谢PetSerAl)。现在sudo.ps1
工作正常:
$w=""; foreach($a in $args[1..($args.length-1)] ){ $w+=" "; if($a -match " "){$w+="""$a"""}else{$w+=$a} }; $w
Start-Process $args[0] -ArgumentList $w -Verb RunAs -Wait
如果寻找更好,更复杂的决策,请考虑Keith Hill和Bill_Stewart建议的其他解决方案。