我正在尝试通过启动进程执行安装,但我希望它作为一个作业执行,这样我就可以执行一些其他语句,同时还可以在后台运行时检查安装的状态。
以下是我要执行的代码的一部分 -
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"" + "$SetupPath\setup.exe" + "`""
$command = "{$SetupProcess=" + "Start-process" + " " + `
"$Setup" + " "+ "-ArgumentList" + " " + `
"/config config.xml" + " " + "-Wait -PassThru" + "}"
# The above command equals-> {$SetupProcess=Start-process "C:\Test Installs\setup.exe" -ArgumentList /config config.xml -Wait -PassThru}
#Change string to Scriptblock
$ScriptBlock = [Scriptblock]::Create($command)
$job1 = Start-Job -ScriptBlock $ScriptBlock -Name "SetupJob"
当我运行它并尝试通过Receieve-Job获取结果时,我只返回通过脚本块传递的命令字符串。看来Start-process命令没有执行。有什么我想念的吗?
答案 0 :(得分:1)
删除$ command定义中的{}
。 [ScriptBlock] :: Create()需要一些 it 将在scriptblock中包装的文本。你也可以简化这个:
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"$SetupPath\setup.exe`""
$command = "SetupProcess = Start-process `"$Setup`" -ArgumentList `"/config config.xml`" -Wait -PassThru"