如何重定向另一个进程创建的进程的标准I / O. [英] How can I redirect standard I/O of a process created by another process
问题描述
我已经使用带有重定向的stdi / o的System.Diagnostics.Process :: start()启动了一个进程。此过程创建另一个过程。我想在powershell中重定向第二个进程的stdi / o。
这是一个示例代码,我在其中运行cmd.exe进程创建的perl脚本使用System.Diagnostics.Process :: start()。我希望在日志文件中看到perl脚本的输出,并将关键笔划发送到perl解释器。
Powershell脚本:
$ LogFilePath =((get-location).ToString()+\ Log.txt);
测试子进程的stdio重定向> $ 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 -SourceIdentifiercmdProcess.ErrorDataReceived-Action {if(![string] :: IsNullOrEmpty($ EventArgs.Data)){$ EventArgs.Data | Add-Content $ global:LogFilePath}}
Register-ObjectEvent $ cmdProcess OutputDataReceived -SourceIdentifiercmdProcess.OutputDataReceived-Action {if(![string] :: IsNullOrEmpty($ EventArgs.Data)){$ EventArgs.Data | Add-Content $ global:LogFilePath}}
$ cmdProcess.start()
Start-Sleep -s 2;
$ cmdProcess.BeginErrorReadLine();
开始睡眠-s 2;
$ cmdProcess.BeginOutputReadLine();
开始睡眠-s 2;
$ cmdProcess.StandardInput.WriteLine(D:\ batt_files\CC_Automation\test\hello.pl);
hello.pl
printHello \ n;
print按Enter键退出;
$ key = < > ;
输出:Log.txt
测试子进程的stdio重定向
Microsoft Windows [版本6.1.7601]
版权所有(c)2009 Microsoft Corporation。版权所有。
C:\ Windows \ System32 \ WindowsPowerShell \v1.0> D:\ batt_files\CC_Automation\test\hello.pl
LogFilePath =((get-location).ToString()+\ Log.txt);
测试子进程的stdio重定向>
LogFilePath;
cmdProcess = New-Object System.Diagnostics.Process;
I have started a process using System.Diagnostics.Process::start() with redirected stdi/o. This process creates another process. I want to redirect the stdi/o of the second process in powershell.
Here is a sample code in which i am running a perl script from cmd.exe process created using System.Diagnostics.Process::start(). I want to see the output of the perl script in the log file as well as send key strokes to the perl interpreter.
Powershell script:
$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") ;
hello.pl
print "Hello\n";
print "press Enter key to exit";
$key = <>;
Output:Log.txt
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
LogFilePath = ((get-location).ToString() + "\Log.txt"); "test stdio redirection of child process" >
LogFilePath;
cmdProcess = New-Object System.Diagnostics.Process;
这篇关于如何重定向另一个进程创建的进程的标准I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!