参数传递到调用命令
问题描述:
我在传递参数问题Invoke-Command
,我已经使用-Args
和-ArgumentList
无济于事尝试。参数传递到调用命令
function One {
$errcode = $args
$username = "Ron"
$password = ConvertTo-SecureString -String "Baxter" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$cred
$Result = Invoke-Command -ComputerName MyPc -ScriptBlock { & cmd.exe /c "C:\Scripts\test.bat" Param1 $errcode ; $lastexitcode} -Credential $cred
echo $result
}
One 10
答
您可以更新功能在参数传递为$errcode
,而不是使用$args
,这是更好的代码,因为它是减少混乱。 (我建议readng了上parameters and functions,因为它一定会帮助)
然后,你需要使用ArgumentList
参数传递到$errcode
Invoke-Command
,并在其位置使用$args[0]
:
function One ($errcode) {
$username = "Ron"
$password = ConvertTo-SecureString -String "Baxter" -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$Result = Invoke-Command -ComputerName MyPc -ScriptBlock { & cmd.exe /c "C:\Scripts\test.bat" Param1 $args[0] ; $lastexitcode} -Credential $cred -ArgumentList $errcode
echo $Result
}
One 10
+0
哦,我的!这位先生是令人愉快的。谢谢。绝对享受。 – RonBurgundy
你得到的错误? –
不是这样,所有返回的是'ECHO已打开'。 – RonBurgundy