[英]Getting stdout to appear on console with PowerShell Invoke-Command and Process
I've got a Powershell script that is making use of Invoke-Command. 我有一个使用Invoke-Command的Powershell脚本。 Here's the code that is being invoked: 这是被调用的代码:
$scriptblock = {
$process = New-Object system.Diagnostics.Process
$si = New-Object System.Diagnostics.ProcessStartInfo
$si.FileName = $cmd
$si.Arguments = $cmd_args
$si.UseShellExecute = false
$si.RedirectStandardOutput = true
$process.StartInfo = $si
$process.Start()
$processId = $process.Id
$process.WaitForExit()
}
Invoke-Command -ScriptBlock $scriptblock
The issue is related to getting the process's standard output to display to the PowerShell console, when the script is run from the PowerShell window. 该问题与从PowerShell窗口运行脚本时使进程的标准输出显示到PowerShell控制台有关。
Now, I am certain that the executable $cmd emits to stdout as when I run the executable with cmd.exe it shows output to that console. 现在,我确定可执行文件$ cmd会发出到stdout,就像我使用cmd.exe运行可执行文件时,它会显示该控制台的输出。 I've also been able to successfully redirect the output when I call this Powershell script from a NodeJS script like so: 从NodeJS脚本调用此Powershell脚本时,我还能够成功重定向输出,如下所示:
var exec = require('child_process').exec,
var cmd = 'powershell -ExecutionPolicy Bypass "' + launch_cmd + " \'" + ver + "\'\"";
console.log("\n\tUsing command for NodeJS child process:\n\t\t" + cmd + "\n\n")
child = exec(cmd, { cwd : prj }, function(stdout, stderr){});
child.stdout.on("data",function(data){
process.stdout.write(data);
});
child.stderr.on("data",function(data){
process.stderr.write(data);
});
child.on("exit",function(){
console.log("\n\nFinished Process");
});
I want to be able to get the StandardOutput to the PowerShell console. 我希望能够将StandardOutput获取到PowerShell控制台。 I reviewed this TechNet documentation and tried the following in the script block, but to no avail: 我查看了此TechNet文档,并在脚本块中尝试了以下操作,但无济于事:
$scriptblock = {
$process = New-Object system.Diagnostics.Process
$si = New-Object System.Diagnostics.ProcessStartInfo
$si.FileName = $cmd
$si.Arguments = $cmd_args
$si.UseShellExecute = false
$si.RedirectStandardOutput = true
$process.StartInfo = $si
$process.Start()
$process.BeginOutputReadLine()
$process.StandardOutput.ReadToEnd()
$processId = $process.Id
$process.StandardOutput
$process.WaitForExit()
}
Invoke-Command -ScriptBlock $scriptblock
Any suggestions as to how I can make the process display its standard output to the PowerShell console? 关于如何使过程将其标准输出显示到PowerShell控制台的任何建议?
You need to continously read the StandardOutput stream until the process exits to get everything: 您需要连续读取StandardOutput流,直到流程退出以获取所有内容:
$scriptblock = {
param($cmd, $cmd_args)
$process = New-Object system.Diagnostics.Process
$si = New-Object System.Diagnostics.ProcessStartInfo
$si.FileName = $cmd
$si.Arguments = $cmd_args
$si.UseShellExecute = $false
$si.RedirectStandardOutput = $true
$process.StartInfo = $si
$process.Start() | Out-Null
do{ # Keep redirecting output until process exits
Write-Host $process.StandardOutput.ReadLine()
} until ($process.HasExited)
}
Invoke-Command -ScriptBlock $scriptblock -ArgumentList "cmd.exe","/c echo test1 & echo test2 & ping 127.0.0.1 -n 3 > nul & echo test3"
You'll see that the output stream is replicated to your host in real-time. 您将看到输出流被实时复制到您的主机。 You could also use Write-Output
if you're interested in passing the results into a pipeline 如果您有兴趣将结果传递到管道中,也可以使用Write-Output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.