【发布时间】:2015-10-27 17:36:21
【问题描述】:
在 powershell 脚本中,我必须运行外部命令。
此命令是一个命令行可执行文件,需要通过键入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'|&"\\path\to\my\executable.exe"? -
@PetSerAl:有效!你应该把它写成答案
标签: powershell