我正在尝试在PowerShell中实现一个如下所示的作业:
$cred = Get-Credential
$job1 = Start-Job -InputObject $cred -ScriptBlock {
Get-ADUser -Credential $cred -Filter *
}
$res1 = Wait-Job -Job $job1 | Receive-Job
但我收到一条错误消息:
等待作业:等待作业cmdlet无法完成工作,因为一个或多个作业被阻止等待用户交互。请使用Receive-Job cmdlet处理交互式作业输出,然后重试。....检测到死锁:(System.Manageme...n.PSRemotingJob:PSRemotingJob) Wait-J ob,....
但是如果我像这样创建这个看似相同的工作:
$job2 = Start-Job -ScriptBlock {
$pass = ConvertTo-SecureString "pass" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "usr",$pass
Get-ADUser -Credential $cred -Filter *
}
$res2 = Wait-Job -Job $job2 | Receive-Job
一切都运行得很完美。
你能告诉我为什么吗?
谢谢!
发布于 2015-10-21 03:19:18
要将参数传递给脚本块,需要使用-ArgumentList
参数,而不是-InputObject
。试试这个:
$cred = Get-Credential
$job1 = Start-Job -ScriptBlock {PARAM($cred)
Get-ADUser -Credential $cred -Filter *
} -ArgumentList $cred
请注意,-ArgumentList
必须是Start-Job
命令中的最后一个参数。
发布于 2015-10-21 03:21:54
如果您只阅读了Start-Job
的帮助,这一点可能会很明显。如果使用-InputObject
参数,则可以使用自动变量$Input
从脚本块中引用它。由于您引用的$Cred
超出了作用域,因此它会再次尝试获取凭据。下面是该参数的帮助文本。
-InputObject <PSObject>
Specifies input to the command. Enter a variable that contains the objects, or type a command or expression that generates the objects.
In the value of the ScriptBlock parameter, use the $input automatic variable to represent the input objects.
您可以将您的脚本修改为如下所示,它应该可以很好地工作:
$cred = Get-Credential
$job1 = Start-Job -InputObject $cred -ScriptBlock {
Get-ADUser -Credential $Input -Filter *
}
$res1 = Wait-Job -Job $job1 | Receive-Job
https://stackoverflow.com/questions/33243714
复制相似问题