在PowerShell腳本中,我必須運行一個外部命令。PowerShell啓動可執行文件,需要輸入「Y」
該命令是一個命令行可執行文件,需要通過鍵入Y
( Do you want to proceed? [Y or N] then press [enter]
)來確認其執行。
不幸的是,沒有靜音開關。
如何運行命令,等待此確認並輸入Y
代替用戶?
我想:
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = "\\path\to\my\executable.exe"
$psi.UseShellExecute = $false
$psi.RedirectStandardInput = $true
$psi.RedirectStandardOutput = $true
$psi.Arguments = '-import -scope "\\path\to\my\scope.xml"'
$p = [System.Diagnostics.Process]::Start($psi)
$line = ""
do{
while($line = $p.StandardOutput.ReadLine()){
Write-Host $line
if($line -Eq "[Y or N] then press [enter]:"){
$p.StandardInput.WriteLine("Y")
}
}
} while(-not $p.HasExited)
但它看起來像馬上進程退出。輸出只是我命令的第一行。
我應該是實際:
Search Migration Tool v1.2.1
Program Action: Import
SharePoint objects considered: Scopes
Conflict Behavior: Continue
Filename: \\path\to\my\scope.xml
Do you want to proceed?
[Y or N] then press [enter]:
但只有Search Migration Tool v1.2.1
顯示
你嘗試:'「Y」 |&」 \\路徑\爲\我\ executable.exe「'? – PetSerAl
@PetSerAl:它的工作原理!你應該把它寫成一個答案 –