Product: PowerShell Universal
Version: 3.9.17
Hello,
I have a problem with scripts executing external process,
When I executing the script it being stuck at “Waiting for pipeline output…” (even when I choose discard pipline at script options)
**In General: how should I execute external command line programs and get their output in PowerShell universal? **
function CopyToLinux {
$LinuxIP = "172.16.12.86"
$Program_PSCP = "D:\Externals\pscp.exe"
$WinPath = '\\fileserver\CSVFile.csv'
$LinuxPath = '/opt/app/etc/apps/search/lookups/'
$Args = "-P 22 -pw $Secret:Linux_Creds.Password `"$($WinPath)`" $($Secret:Linux_Creds.Username)@$($LinuxIP):`"$($LinuxPath)`""
$LinuxFileInfo = Get-ChildItem -File $WinPath
$lastupdatetime=$LinuxFileInfo.LastWriteTime
$nowtime = get-date
if (($nowtime - $lastupdatetime).totalhours -le 24) {
Invoke-Process -FilePath $Program_PSCP -ArgumentList $Args
}
}
CopyToLinux
at first I try to use Invoke-Expression but with same results, next I try to use Invoke-Proccess downloaded from psGallery But same results.
The Invoke-Process Function:
function Invoke-Process {
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FilePath,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ArgumentList
)
$ErrorActionPreference = 'Stop'
try {
$stdOutTempFile = "$env:TEMP\$((New-Guid).Guid)"
$stdErrTempFile = "$env:TEMP\$((New-Guid).Guid)"
$startProcessParams = @{
FilePath = $FilePath
ArgumentList = $ArgumentList
RedirectStandardError = $stdErrTempFile
RedirectStandardOutput = $stdOutTempFile
Wait = $true;
PassThru = $true;
NoNewWindow = $true;
}
if ($PSCmdlet.ShouldProcess("Process [$($FilePath)]", "Run with args: [$($ArgumentList)]")) {
$cmd = Start-Process @startProcessParams
$cmdOutput = Get-Content -Path $stdOutTempFile -Raw
$cmdError = Get-Content -Path $stdErrTempFile -Raw
if ($cmd.ExitCode -ne 0) {
if ($cmdError) {
throw $cmdError.Trim()
}
if ($cmdOutput) {
throw $cmdOutput.Trim()
}
} else {
if ([string]::IsNullOrEmpty($cmdOutput) -eq $false) {
Write-Output -InputObject $cmdOutput
}
}
}
} catch {
$PSCmdlet.ThrowTerminatingError($_)
} finally {
Remove-Item -Path $stdOutTempFile, $stdErrTempFile -Force -ErrorAction Ignore
}
}