Start-Process -RedirectStandardOutput issues

udhayan d 181 Reputation points
2022-05-25T11:19:46.037+00:00

Hi Experts,

I am trying to redirect the output and error separately to two different files when i use start-process to run PostgreSQL base backup. But std output also goes to the error file.
"-P" switch outputs the progress of PG_basebackup and i want that std ouput to go to the output file and not the error file.

Start-Process -FilePath "C:\Program Files\PostgreSQL\14\bin\pg_basebackup.exe" -ArgumentList "-D G:\FULL_Backup\Test", "-Ft", "-z", "-R", "-U pgbackup", "-w", "-P" -Wait -NoNewWindow -RedirectStandardOutput "G:\FULL_Backup\PG_Basebackup_Logs\pg_bb_test.log" -RedirectStandardError "G:\FULL_Backup\PG_Basebackup_Logs\pg_bb_err_test.log"

205429-image.png

I simulated an error and the error goes to error file correctly

205501-image.png

Thanks

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 45,176 Reputation points
    2022-05-30T07:26:06.01+00:00

    Hello Udhay,

    Thanks for posting your query here and we are happy to assist you regarding the PowerShell command.

    The Start-Process cmdlet starts one or more processes on the local computer. By default, Start-Process creates a new process that inherits all the environment variables that are defined in the current process.

    You can use following command to redirect the output / error to each file. Try placing your command to run PostgreSQL exe in input file. Following code works for me.

    $processOptions = @{
    FilePath = "ping.exe"
    RedirectStandardInput = "C:\Users\O00\OneDrive\Desktop\PS-Scripts\TestSort.txt"
    RedirectStandardOutput = "C:\Users\O00\OneDrive\Desktop\PS-Scripts\Sorted.txt"
    RedirectStandardError = "C:\Users\O00\OneDrive\Desktop\PS-Scripts\SortError.txt"
    UseNewEnvironment = $true
    }
    Start-Process @processOptions

    Please find additional information in below link.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.2

    Also try following option. Instead of returning the data to host, try to redirect to any file.

    $pinfo = New-Object System.Diagnostics.ProcessStartInfo
    $pinfo.FileName = "C:\Program Files\PostgreSQL\14\bin\pg_basebackup.exe"
    $pinfo.RedirectStandardError = $true
    $pinfo.RedirectStandardOutput = $true
    $pinfo.UseShellExecute = $false
    $pinfo.Arguments = "-D G:\FULL_Backup\Test", "-Ft", "-z", "-R", "-U pgbackup", "-w", "-P"
    $p = New-Object System.Diagnostics.Process
    $p.StartInfo = $pinfo
    $p.Start() | Out-Null
    $p.WaitForExit()
    $stdout = $p.StandardOutput.ReadToEnd()
    $stderr = $p.StandardError.ReadToEnd()
    Write-Host "stdout: $stdout"
    Write-Host "stderr: $stderr"
    Write-Host "exit code: " + $p.ExitCode

    Please feel to reply if you have any additional questions.

    Here is a helpful page with information about Windows PowerShell

    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/powershell

    ---------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer.--

    0 comments No comments

  2. Rich Matheisen 48,036 Reputation points
    2022-05-25T14:55:46.687+00:00

    Not having access to the software you're using I can only ask questions.

    If you placed the C:\Program Files\PostgreSQL\14\bin\pg_basebackup.exe -D G:\FULL_Backup\Test -Ft -z -R -U pgbackup -w -P into a BAT file and added the appropriate redirect of STDOUT and STDERR ( 2>G:\FULL_Backup\PG_Basebackup_Logs\pg_bb_err_test.log and 1>G:\FULL_Backup\PG_Basebackup_Logs\pg_bb_test.log ), do the streams go where you expect them to?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.