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
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.
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");
});
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
Any suggestions as to how I can make the process display its standard output to the PowerShell console?