$LogFilePath = ((get-location).ToString() + "\Log.txt"); "test stdio redirection of child process" > $LogFilePath; $cmdProcess = New-Object System.Diagnostics.Process; $cmdProcess.StartInfo.FileName = "cmd.exe" $cmdProcess.StartInfo.UseShellExecute = $false; $cmdProcess.StartInfo.RedirectStandardInput = $true; $cmdProcess.StartInfo.RedirectStandardOutput = $true; $cmdProcess.StartInfo.RedirectStandardError = $true; Register-ObjectEvent $cmdProcess ErrorDataReceived -SourceIdentifier "cmdProcess.ErrorDataReceived" -Action { if(![string]::IsNullOrEmpty($EventArgs.Data)) { $EventArgs.Data | Add-Content $global:LogFilePath } } Register-ObjectEvent $cmdProcess OutputDataReceived -SourceIdentifier "cmdProcess.OutputDataReceived" -Action { if(![string]::IsNullOrEmpty($EventArgs.Data)) { $EventArgs.Data | Add-Content $global:LogFilePath } } $cmdProcess.start() Start-Sleep -s 2 ; $cmdProcess.BeginErrorReadLine(); Start-Sleep -s 2 ; $cmdProcess.BeginOutputReadLine(); Start-Sleep -s 2 ; $cmdProcess.StandardInput.WriteLine("D:\batch_files\CC_Automation\test\hello.pl") ;
print "Hello\n"; print "press Enter key to exit"; $key = <>;
test stdio redirection of child process Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Windows\System32\WindowsPowerShell\v1.0>D:\batch_files\CC_Automation\test\hello.pl
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)