PowerShell pass arguments with quotes -
have useful script named sudo.ps1
:
$w=""; foreach($a in $args[1..($args.length-1)] ){ $w += " " + $a }; $w start-process $args[0] -argumentlist $w -verb runas -wait
but can't handle complex command
./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) , (eventid=20225)]] , *[eventdata[data='$vpn_name']]" /rl highest
problem in obfuscated quotes. in sudo.ps1
quotes opened:
/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) , (eventid=20225)]] , *[eventdata[data='vpn-kosmos6']] /rl highest
command executed no error, no work. how can fixed?
if arg contains space, add quotes via """
(thanks petseral). sudo.ps1
works fine:
$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
consider solutions suggested keith hill , bill_stewart, if looking better , more complex decision.
Comments
Post a Comment