【发布时间】:2017-04-19 03:38:13
【问题描述】:
我有下面的脚本没有按我想要的方式工作。最初,我想将 install.cmd 传递给将在后台使用“Start-Job”的函数,这样它就不会冻结主 Powershell 窗口。但我无法让它调用 install.cmd。
$Appname = @("Adobe_FlashPlayer", "Acrobat_Reader", "Microsoft_RDP")
function BatchJob{
Param (
[ScriptBlock]$batchScript,
$ArgumentList = $null)
#Start the batch
$batch = Start-Job -ScriptBlock $batchScript -ArgumentList $ArgumentList
}
Foreach($App in $Appname){
$Install = "C:\test\$App\Install.cmd"
Batchjob -batchscript {Invoke-Command (cmd / c)} -ArgumentList $install
Wait-Job $job
Receive-Job $job
}
【问题讨论】:
-
您定义了
$Install = "C:\test\$App\Install.cmd",但从不在此之外引用它。我认为您需要将Invoke-Command设为{Invoke-Command "cmd.exe /c $install"}您会遇到问题,因为(假设有多个应用程序使用 Windows Installer)当您尝试启动时,Windows Installer 将忙于第一个应用程序安装第二个(和第三个等)以及第一个之后的应用程序将失败。 -
啊我实际上忘记在“-argumentlist”末尾添加 $install 请参阅上面的更新代码。我尝试了您的代码,但它只是给了我“从上述路径调用 cmd...”的错误。一如既往地感谢 Madtechnician。
-
您的脚本块
{Invoke-Command (cmd / c)}没有定义的参数,您可以将该参数传递给该参数。您到底希望它对参数列表做什么?它是否应该本质上知道将其附加到传递给Invoke-Command的命令的末尾?