'Powershell: system-diagnostics-processstartinfo hangs, reason unknown

Highly influenced by other questions here on Stackoverflow I have ended up with this method for starting processes from my Powershell-scripts

function global:system-diagnostics-processstartinfo {
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
    [Parameter(Mandatory=$True,HelpMessage='Full path to exectuable')]
    [Alias('exectuable')]
    [string]$exe,
    [Parameter(Mandatory=$True,HelpMessage='All arguments to be sent to exectuable')]
    [Alias('args')]
    [string]$arguments
)

if (!(Test-Path $exe)) {
    $log.errorFormat("Did not find exectuable={0}, aborting script", $exe)
    exit 1  
}

$log.infoFormat("Start exectuable={0} with arguments='{1}'", $exe, $arguments)
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo($exe)
$processStartInfo.FileName = $exe
$processStartInfo.RedirectStandardError = $true
$processStartInfo.RedirectStandardOutput = $true
$processStartInfo.UseShellExecute = $false
$processStartInfo.Arguments = $arguments
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $processStartInfo
$log.info("Start exectuable and wait for exit")
$p.Start() | Out-Null

#$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
$log.infoFormat("exectuable={0}  stdout: {1}", $exe, $stdout)
$log.debugFormat("exectuable={0} stderr: {1}",  $exe,$stderr)
$global:ExitCode = $p.ExitCode
$log.debugFormat("exectuable={0} Exitcode: {1}", $exe, $p.ExitCode)

return $stdout
}

Pretty straight forward with some added logging etc. And it works in all my current use cases execpt one. I have created a script that copies the database dump for our production instance of Confluence to our test server. Then it uses the above method to drop existing database, all fine. But the actual restore just hangs for ever and ever. So right now I have to exit the script and then run the following command manually

d:\postgresql\bin\pg_restore.exe -U postgres -d confluencedb -v -1 d:\temp\latest-backup.pgdump

It takes some time and there is quite a lot of output. Which makes me belive that there must be either one the following causing the issue

  • The amount of output makes a buffer overflow and stalls the script
  • It takes to much time

Anyone with similar experiences who can help me resolve this. It would enable to schedule the import, not having to do it manually as today.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source