I have a PowerShell scripts, the basic function is to run curl.exe to copy a file from remote ftp server to local. At first, I used the following code to run curl.exe:
$curlPath = "curl.exe"
$curlArgs = "sftp://download.url.com/filename_20220618.zip --user username:psw -o D:\filename_20220618.zip -k"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $curlPath
$pinfo.Arguments = $curlArgs
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.CreateNoWindow = $true
$curlProcess = New-Object System.Diagnostics.Process
$curlProcess.StartInfo = $pinfo
$curlProcess.Start() | Out-Null
$curlProcess.WaitForExit()
The code sometimes works. But most of the time will pending forever. I don't known the cause. When the code works, I can get error message from $curlProcess.StandError if $curlProcess.ExitCode is not 0.
To work around, I decided to use
$curlPath = "curl.exe"
$curlArgs = "sftp://download.url.com/filename_20220618.zip --user username:psw -o D:\filename_20220618.zip -k"
$curlProcess = Start-Process $curlPath -ArgumentList $curlArgs -Wait -PassThru -WindowStyle Hidden
The above codes worked. The only problem is when $curlProcess.ExistCode is not 0, I cannot get the error message. $curlProcess.StandError is always empty.
Does anyone know how to fix this issue?