YTZ
YTZ

Reputation: 938

Powershell fails to wait for curl to complete upload

I'm using curl to upload a file to the cloud. The problem is that, the script immediately removes the local file before the curl operation was completed. The weird thing is when I put the & curl.exe and Remove-Item in a separate ps1 file and test it there, it works properly and the file is removed after uploading it.

The following code is from main.ps1 (executed on start-up from cmd), which checks my inputfolder every 5 seconds for new files

while ($true) {
    Get-ChildItem -Path $inputFolder -Filter "*$extIn" | ForEach-Object {

        & ffmpeg.exe -i $inFile ...some operations $outFile

        if ($LASTEXITCODE -eq 0) {
            #credentials
            $lg = ...
            #curl upload operation
            & curl.exe -T "$outFile" -g -u $lg -k https://mycloud/
            Write-Host "Done uploading: $outFile"
            #removing item
            Remove-Item -LiteralPath $inFile -Force
            Write-Host "Removed input file!"
        } else {
            Write-Error "Conversion failed!"
        } 
    }
    sleep 5
}

I used curl.exe because it gave me an error when I used only curl (I don't know if this is relevant):

Invoke-WebRequest : Parameter cannot be processed because the parameter name 'T' is ambiguous. Possible matches include ...

On a sidenote: I tried the same script (but using curl -T ...) on my other pc which runs on ubuntu 16.04. No problems there. The script will eventually be used on a ubuntu 16.04/17.10, but in the meantime it needs to work on my windows pc.

Any bit of help is appreciated.

Upvotes: 0

Views: 4093

Answers (2)

postanote
postanote

Reputation: 16096

As for: "I used curl.exe because it gave me an error when I used only curl (I don't know if this is relevant):"

It is because in PoSH commands like curl, wget are all aliases for PoSH cmdlets. You can use these commands but you have to use what PoSH allows.

Get-Alias -Name curl
CommandType     Name 
Alias           curl -> Invoke-WebRequest

(Get-Command -Name Invoke-WebRequest).Parameters
Get-Help -Name Invoke-WebRequest -Examples
Get-Help -Name Invoke-WebRequest -Full

PoSH has a lots of aliases, and as you discovered, you have to use an .exe to get the real thing or remove the alias. PoSH will always use native cmdlets first before making any call to an external command, unless you tell it to.

Show all aliases

Get-Alias

Consider using PoSH WorkFlow

Getting Started with Windows PowerShell Workflow https://technet.microsoft.com/en-us/library/jj134242(v=ws.11).aspx

A workflow is a sequence of programmed, connected steps that perform long-running tasks or require the coordination of multiple steps across multiple devices or managed nodes. Windows PowerShell Workflow lets IT pros and developers author sequences of multi-device management activities, or single tasks within a workflow, as workflows. By design, workflows can be long-running, repeatable, frequent, parallelizable, interruptible, stoppable, and restartable. They can be suspended and resumed; they can also continue after an unexpected interruption, such as a network outage or computer restart.

Windows PowerShell: PowerShell scripts versus PowerShell workflows https://technet.microsoft.com/en-us/library/dn151046.aspx

PowerShell Workflows: Nesting https://blogs.technet.microsoft.com/heyscriptingguy/2013/01/09/powershell-workflows-nesting

Upvotes: 0

Allen Tellez
Allen Tellez

Reputation: 1211

Can try

Start-Process <path to exe> -NoNewWindow -Wait

or

$proc = Start-Process <path to exe> -NoWindow
$proc.WaitForExit()

Either of these should wait for the process to complete. You can also try adding this to the end of your line where calling curl, a pipe and Wait-Process

| Wait-Process

Upvotes: 1

Related Questions