
我有一些命令只能在前綴為&
.
它們都是非同步運作的。
範例腳本:
& 'C:\Program Files\SomeApp\SomeAppUninstall.exe'/silent
& 'C:\Users\bob\Downloads\SomeApp.exe' /silent
& 'C:\Program Files\someApp.exe' /openGui
這裡發生的情況是最後一個命令將首先發生,因為它是最快的,並且程式仍然安裝,前兩個命令將同時運行,導致衝突。
顯然我需要它們按順序運行。
由於某種原因,這些命令只有在帶有前綴的情況下才有效&
,或者我收到錯誤,我懷疑這是因為它們有開關。
我該如何解決這個問題?
答案1
幾年前,我編寫了一個 Powershell 函數,因為我有與您相同或至少相似的需求。如果我沒記錯的話,這個功能對我很有用——儘管我相信我曾經遇到一個我無法解釋的問題。因此,使用風險自負。
function wait-for-process {
param (
[string] $exe,
[string] $arguments
)
$process = new-object System.Diagnostics.Process
$process.StartInfo = new-object System.Diagnostics.ProcessStartInfo
$process.StartInfo.FileName = $exe
$process.StartInfo.Arguments = $arguments
if (-not $process.Start()) {
"Could not start $exe $arguments!"
return
}
$process.WaitForExit()
if ($process.ExitCode) {
"Exit code for $exe $arguments is $($process.ExitCode)"
}
}
答案2
我給的例子非常簡單,實際情況有點複雜,但我的案例都只使用 1 個參數。
我寫了這個簡單的函數:
function Await($path, $arg) {
Start-Process `
-Wait `
-FilePath $path `
-ArgumentList /$arg
}