当命令行参数随变量一起提供时,Powershell 命令调用不起作用
Powershell command call doesn't work when command line arguments are supplied with a variable
我正在尝试为“net accounts”命令提供命令行参数。这在写出不带变量的命令时有效,但在传递带变量的命令行参数时无效。
这是一个演示脚本:
Write-Host "--- As variable"
$Sw = "/lockoutduration:15 /lockoutwindow:15"
Write-Host $Sw
&net.exe accounts $Sw
Write-Host "--- Without variable"
&net.exe accounts /lockoutduration:15 /lockoutwindow:15
输出:
> .\Test.ps1
--- As variable
/lockoutduration:15 /lockoutwindow:15
You entered an invalid value for the /LOCKOUTDURATION option.
More help is available by typing NET HELPMSG 3952.
--- Without variable
The command completed successfully.
提供一组命令行参数的方法是什么?
OS: Windows 10 Pro 20H2, Powershell 版本: 5.1
向程序发送多个参数时,您需要将它们作为数组传递。尝试:
Write-Host "--- As variable"
$Sw = @("/lockoutduration:15","/lockoutwindow:15")
$Sw
&net.exe accounts $Sw
我正在尝试为“net accounts”命令提供命令行参数。这在写出不带变量的命令时有效,但在传递带变量的命令行参数时无效。
这是一个演示脚本:
Write-Host "--- As variable"
$Sw = "/lockoutduration:15 /lockoutwindow:15"
Write-Host $Sw
&net.exe accounts $Sw
Write-Host "--- Without variable"
&net.exe accounts /lockoutduration:15 /lockoutwindow:15
输出:
> .\Test.ps1
--- As variable
/lockoutduration:15 /lockoutwindow:15
You entered an invalid value for the /LOCKOUTDURATION option.
More help is available by typing NET HELPMSG 3952.
--- Without variable
The command completed successfully.
提供一组命令行参数的方法是什么?
OS: Windows 10 Pro 20H2, Powershell 版本: 5.1
向程序发送多个参数时,您需要将它们作为数组传递。尝试:
Write-Host "--- As variable"
$Sw = @("/lockoutduration:15","/lockoutwindow:15")
$Sw
&net.exe accounts $Sw