使用 PowerShell Invoke-Command 和 Process 使 stdout 出现在控制台上 [英] Getting stdout to appear on console with PowerShell Invoke-Command and Process

查看:23
本文介绍了使用 PowerShell Invoke-Command 和 Process 使 stdout 出现在控制台上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Invoke-Command 的 Powershell 脚本.这是正在调用的代码:

I've got a Powershell script that is making use of Invoke-Command. 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

该问题与从 PowerShell 窗口运行脚本时让进程的标准输出显示到 PowerShell 控制台有关.

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.

现在,我确定可执行文件 $cmd 会发送到标准输出,因为当我使用 cmd.exe 运行可执行文件时,它会向该控制台显示输出.当我从 NodeJS 脚本调用这个 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. I've also been able to successfully redirect the output when I call this Powershell script from a NodeJS script like so:

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");
});

我希望能够将 StandardOutput 发送到 PowerShell 控制台.我查看了此 TechNet 文档 并在脚本块中尝试了以下操作,但无济于事:

I want to be able to get the StandardOutput to the PowerShell console. I reviewed this TechNet documentation and tried the following in the script block, but to no avail:

$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

关于如何让进程将其标准输出显示到 PowerShell 控制台的任何建议?

Any suggestions as to how I can make the process display its standard output to the PowerShell console?

推荐答案

您需要不断读取 StandardOutput 流,直到进程退出才能获取所有内容:

You need to continously read the StandardOutput stream until the process exits to get everything:

$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"

您会看到输出流被实时复制到您的主机.如果您有兴趣将结果传递到管道中,也可以使用 Write-Output

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

这篇关于使用 PowerShell Invoke-Command 和 Process 使 stdout 出现在控制台上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆