$ Username =" user"
$Password = "Password"
$servers = get-content "c:\temp\servers.txt"
$sb = {c:\temp\PsExec.exe -h \\$server -u $Username -p $password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$env:userprofile\Desktop\output.txt"}
foreach($server in $servers)
{
start-job -ScriptBlock $sb
}
此代码永不退出。它停在这个位置:
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
答案 0 :(得分:0)
首先,您不会将任何变量传递到作业中。你需要的是在ScriptBlock中使用$ args变量,然后使用-ArgumentList传递你想要的变量。
$Password = "Password"
$servers = get-content "c:\temp\servers.txt"
$sb = {
c:\temp\PsExec.exe -h \\$args[0] -u $args[1] -p $args[2] cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$args[3]\Desktop\output.txt"
}
foreach($server in $servers)
{
start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}
我可能不需要传递环境变量,但看起来你对变量存在范围问题。
或者你可以在ScriptBlock中使用Param Block来命名你的变量,它实际上是在位置上映射传递给命名变量的Arguments。
$Password = "Password"
$servers = get-content "c:\temp\servers.txt"
$sb = {
Param ($Server,$UserName,$Password,$UserProfile)
c:\temp\PsExec.exe -h \\$Server -u $UserName -p $Password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$UserProfile\Desktop\output.txt"
}
foreach($server in $servers)
{
start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}
我希望这会有所帮助。 干杯,克里斯。