2013-11-22 64 views
0

我一直用這個代碼從這個網站的其他職位時:的PowerShell 3.0的行爲讓外部進程退出代碼

$pinfo = New-Object System.Diagnostics.ProcessStartInfo 
$pinfo.FileName = "notepad.exe" 
$pinfo.RedirectStandardError = $true 
$pinfo.RedirectStandardOutput = $true 
$pinfo.UseShellExecute = $false 
$pinfo.Arguments = "" 
$p = New-Object System.Diagnostics.Process 
$p.StartInfo = $pinfo 
**$p.Start() | Out-Null** 
#Do Other Stuff Here.... 
**$p.WaitForExit()** 
$p.ExitCode 

它的PowerShell 2.0下正常工作。服務器升級到PowerShell的3.0,現在這兩個大膽的喜歡失敗:

Exception calling "Start" with "0" argument(s): "The system cannot find the 
file specified" 
At \\asdnsom3978\optim_windows\script_master\exportall.ps1:126 char:1 
+ $p.Start() | Out-Null 
+ ~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : Win32Exception 

Exception calling "WaitForExit" with "0" argument(s): "No process is 
associated with this object." 
At \\asdnsom3978\optim_windows\script_master\exportall.ps1:128 char:1 
+ $p.WaitForExit() 
+ ~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : InvalidOperationException 

爲什麼它打破了,我如何糾正它,所以它同時適用於2.0和3.0版?

回答

2

嗯,我不能repro在我的V3系統上的錯誤。但是,爲什麼你要這麼麻煩時,你可以使用Start-Process命令來做到這一點例如爲:

$p = Start-Process Notepad -Wait -PassThru 
$p.ExitCode 
+0

我複製的代碼,所以我沒有做多少改動。我會嘗試啓動過程,看看會發生什麼。 我的工作站是v2,代碼運行。服務器已從V2升級到V3並引發錯誤。另一臺升級到V3的工作站會引發相同的錯誤。這導致我得出結論這是一個V2/V3的事情。 – user3023064

+0

我剛完成一個測試,這個啓動過程同時適用於V2工作站和V3服務器。謝謝! – user3023064

1

您可能想嘗試提供notepad.exe的完全限定路徑,如「C:\ Windows \ notepad.exe」。