1

I'm trying to figure out how to make AzCopy work with PS command "Start-Process" in PS script. I am trying to put together a PS script that downloads a folder named "Languages" from Azure Storage Blob Container to local disk using SAS token.

My script looks as following:

Start-Process `
    -FilePath "C:\temp\AzCopy\azcopy.exe" `
    -ArgumentList "cp 'https://<storageaccountname>.blob.core.windows.net/<blobcontainername>/Languages?<SASTOKEN>' 'C:\Temp' --recursive" `
    -Wait `
    -Passthru

this syntax gives me the following output:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                                                                         
-------  ------    -----      -----     ------     --  -- -----------                                                                                                                         
     19       3     1492       2004       0,00   2520   1 azcopy       

which immediately exits and nothing happens. If I run AzCopy manually as:

azcopy cp 'https://<storageaccountname>.blob.core.windows.net/<blobcontainername>/Languages?<SASTOKEN>' 'C:\Temp' --recursive

it works as expected.

Michael
  • 13
  • 4

1 Answers1

3

There is no good reason to use Start-Process to invoke console applications - just invoke them directly, as in your second code snippet.

By default, Start-Process produces no output, and (on Windows) runs in a new console window.

With -PassThru it outputs a process-information object representing the launched process, i.e. an instance of System.Diagnostics.Process, and the output shown in your question is the default output formatting for such an instance.

Use of Start-Process does not allow returning the launched process' output; the only way to do that is to use the -RedirectStandardOutput and/or -RedirectStandardError parameters to redirect the process' native output streams to - invariably text-only - files.

By contrast, direct invocation maps the process' stdout stream to PowerShell's equivalent success output stream and, on demand, via a 2> redirection, allows redirecting the stderr stream.

For guidance on when use of Start-Process is and isn't appropriate, see this GitHub docs issue.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Excellent answer as usual of course! What about that wait [`-wait`] factor considering needing that to complete then when it is done, do something else non-`azcopy` but it depending on that to complete first? That's the only "maybe" good reason perhaps but I'm sure you know a way to handle that too!! – Bitcoin Murderous Maniac Jan 20 '23 at 00:38
  • 1
    Thank you, @BitcoinMurderousManiac. _Console[-subsystem]_ applications on Windows invariably execute _synchronously_ (blocking) when you invoke them _directly_. Only _GUI[-subsystem]_ applications require use of either (a) `Start-Process -Wait` _or_ (b) a direct-invocation _workaround_ such as piping to `| Out-Null` or `| Write-Output` - see [this answer](https://stackoverflow.com/a/45131324/45375). – mklement0 Jan 20 '23 at 00:50