在我使用这样的代码之前$prog="cmd.exe";
$params=@('/C','"D:\EmailConnector-Disc Optimus\run.bat"','connector.log')
$rc=start-process $prog $params -WorkingDirectory "D:\EmailConnector-Disc Optimus" -RedirectStandardOutput $emailconnecter_log -PassThru -wait
问题在于这段代码并没有引发错误代码。后来我使用了下面的代码
$errorlog = "D:\EmailConnector-Disc Optimus\logs\error.log"
$emailconnecter_log = "D:\EmailConnector-Disc Optimus\logs\connector.log"
$prog="cmd.exe"
$params=@('/C','"D:\EmailConnector-Disc Optimus\run.bat"','connector.log')
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $prog
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $emailconnecter_log
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $params
$pinfo.WorkingDirectory = "D:\EmailConnector-Disc Optimus"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode
if ($p.ExitCode -gt 0)
{Some code is here }
现在问题是$p.WaitForExit()
没有退出,我是通过PowershellISE调试的。我不知道为什么代码没有退出。另外,我想将控制台输出写入$emailconnecter_log
。以前除返回代码外一切正常。请帮助我。