Home > Net >  AzCopy not working through Start-Process in PS Script
AzCopy not working through Start-Process in PS Script

Time:01-20

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.

CodePudding user response:

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.

  • Related