gpt4 book ai didi

PowerShell 传递带引号的参数

转载 作者:行者123 更新时间:2023-12-03 09:56:47 24 4
gpt4 key购买 nike

有一个非常有用的脚本,名为 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

命令执行没有错误,但不起作用。如何修复?

最佳答案

如果 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 ,如果要寻找更好更复杂的决定。

关于PowerShell 传递带引号的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com