简体   繁体   English

使用PowerShell Invoke-Command和Process使stdout出现在控制台上

[英]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.

相关问题 如何在node.js上运行带有子进程的top命令,而不会从终端获取'stdout maxBuffer exceeded' - How do I run the top command with a child process on node.js without getting a 'stdout maxBuffer exceeded' from the terminal Process.stdout.write将“ true”附加到控制台输出 - Process.stdout.write appends “true” to the console output Console.log()和process.stdout.write是否不同? - Is Console.log() and process.stdout.write different? Nodejs child_process.exec:禁用在控制台上打印标准输出 - Nodejs child_process.exec : Disable printing of stdout on console Nodejs exec子进程stdout没有得到所有的块 - Nodejs exec child process stdout not getting all the chunks 如何从console.log(require(“ child_process”)。execSync('ls'))获取文本标准输出? - how to get textual stdout from console.log(require(“child_process”).execSync('ls'))? 使用Mocha从异步功能测试控制台输出(process.stdout.write) - Testing console output (process.stdout.write) from async functions using Mocha node.js 中“process.stdout.write”和“console.log”的区别? - Difference between "process.stdout.write" and "console.log" in node.js? console.log和process.stdout.write中的节点流缓冲区 - Node stream buffers in console.log vs process.stdout.write 子进程:stdout参数 - Child process: The stdout parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM