Azure / azure-storage-azcopy

The new Azure Storage data transfer utility - AzCopy v10
MIT License
614 stars 222 forks source link

Feature request: Azcopy powershell doesn't have return code when copy doesn't happen #1105

Open gsreevastava opened 4 years ago

gsreevastava commented 4 years ago

Which version of the AzCopy was used?

Note: The version is visible when running AzCopy without any argument
10.5.0

Which platform are you using? (ex: Windows, Mac, Linux)

Linux

What command did you run?

Note: Please remove the SAS to avoid exposing your credentials. If you cannot remember the exact command, please retrieve it from the beginning of the log file.
azcopy cp "https://[account].blob.core.windows.net/[container]/[path/to/directory]?[SAS]" "/path/to/dir" --recursive=true

What problem was encountered?

There is no return code to check the logic condition whether the transfer is successful or not in powershell. for example if the sastoken is missing from the above command, it doesn't throw an error rather throws an informational error kind of message and executes the subsequent command as we cannot use $ErrorActionPreference = 'Stop' to stop the next line execution within powershell script

INFO: Scanning...

failed to perform copy command due to error: no SAS token or OAuth token is present and the resource is not public

How can we reproduce the problem in the simplest way?

$ErrorActionPreference = 'Stop'
azcopy cp "https://[account].blob.core.windows.net/[container]/[path/to/directory]?[bad_SAS_token/missing_SAS_token]" "/path/to/dir" --recursive=true

Have you found a mitigation/solution?

No

zezha-msft commented 4 years ago

@nakulkar-msft are you familiar with Powershell? Could you please take a look?

gsreevastava commented 4 years ago

@zezha-msft - what info you need ?

bahadir-be commented 3 years ago

@zezha-msft I have similar issue. how can we capture azcopy errors?

zezha-msft commented 3 years ago

@gsreevastava @bahadir-be sorry I'm not very familiar with PowerShell, are you saying that you want AzCopy to raise exceptions in PowerShell?

For context, AzCopy is a cross-platform cmdline tool, so in general we have no specific feature for any shell.

bahadir-be commented 3 years ago

@gsreevastava @bahadir-be sorry I'm not very familiar with PowerShell, are you saying that you want AzCopy to raise exceptions in PowerShell?

For context, AzCopy is a cross-platform cmdline tool, so in general we have no specific feature for any shell.

@zezha-msft
Concernig Exception Handling

I have installed the azcopy on my local system and I am able to run the azcopy commands in Windows Powershell. When I run any command which is not correct or throwing any exception, I am able to see it on the console. ( ex : expiry SAS token,wrong path ..etc )

But how can I capture this exception using Try...Catch . I want to handle the exception using try..catch in powershell script.

zezha-msft commented 3 years ago

Thanks @bahadir-be for the explanation. We need to do a bit of digging to figure out how difficult or appropriate this is. My intuition is that we most likely won't be able to add shell-specific behavior.

Could you assess the exit code instead?

bahadir-be commented 3 years ago

Thanks @bahadir-be for the explanation. We need to do a bit of digging to figure out how difficult or appropriate this is. My intuition is that we most likely won't be able to add shell-specific behavior.

Could you assess the exit code instead?

Unfortunately no. I have used below trick for temporary solution..

` $SASOutput = D:\DumpTools\azcopy.exe copy --log-level $AzCopyLogValue ($CrashDumpZIPpath + "\" + $NameZipFile) ($uri + $NameZipFile + $SASToken) --check-length=false $SASOutput $ErrorSAS = $SASOutput -match 'ERROR'

     if( $SASOutput -match 'ERROR')
    {
       Write-Host "An error occurred [SAS] : $ErrorSAS"
       SendEmailNotification -EMAILTO "$SupportEmail" -EMAILSUBJECT "CrashDUMP [ ERROR ] Notification // $env:COMPUTERNAME" -EMAILBODY "An error occurred [CrashDUMP] : $ErrorSAS"
       Write-Host "Email notification has been successfully sent!" -BackgroundColor red -ForegroundColor white

       return
    }
    else {
        Write-Host "[SAS] Continue..." -ForegroundColor Magenta
    }

`

haidouks commented 2 years ago

Maybe something like below works for you as your platform is Linux and you use powershell core

azcopy cp "$uri" "$path" --recursive=true || Throw $_

Also it can be useful to check last operation result.

azcopy cp "$uri" "$path" --recursive=true
$? # Will return $false if last operation resulted in an error. 
asvyat commented 1 year ago

would be helpful to fix this issue of returning proper error code from AZCOPY for PS scripting or automation.

amangarg1996 commented 1 year ago

We have used the below approach for Error Handling :

Set the .exe file path

$azcopyPath = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy\AzCopy.exe"

To terminate the script

$ErrorActionPreference = "Stop"

try {

azcopy command

& $azcopyPath /Source:$source /Dest:$destination

Write-Host 'Successfully' }

catch { Write-Host "Error: $_" exit 1 }

The above code will move in the CATCH block to capture the error.

itenginex commented 1 year ago

I have the same on a linux system, getting 403 error, but exit code is 0. Also does not raise any exception that can be handled by the calling powershell. Proposed solution by amangarg1996 does not work in my case.

shurick81 commented 8 months ago

@amangarg1996 what errors is this approach catching and what azcopy version is it that you are using?

Trying these two azcopy lines and none of them catches anything:

  Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile azcopy.zip;
  Expand-Archive -Path azcopy.zip -DestinationPath .;
  Copy-Item azcopy_*/azcopy.exe azcopy.exe;
  $ErrorActionPreference = "Stop";
  try {
    azcopy.exe cp test https://test;
  } catch {
    Write-Host "##vso[task.logissue type=warning;]this is a warning";
    Write-Host "Error _";
    Write-Host "Error $_";
    Write-Host "Error 0";
    Throw $Error[0];
  }
  try {
    azcopy.exe;
  } catch {
    Write-Host "##vso[task.logissue type=warning;]this is a warning";
    Write-Host "Error _";
    Write-Host "Error $_";
    Write-Host "Error 0";
    Throw $Error[0];
  }
shurick81 commented 8 months ago

This is what worked for me:

Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile azcopy.zip;
Expand-Archive -Path azcopy.zip -DestinationPath .;
Copy-Item azcopy_*/azcopy.exe azcopy.exe;
azcopy.exe cp test https://test;
if ( $LastExitCode -ne 0 ) {
  Write-Host "Error when copying to test";
}
azcopy.exe;
if ( $LastExitCode -ne 0 ) {
  Write-Host "Error when just running azcopy";
}

it catches error when running azcopy.exe cp test https://test but no errors when just running azcopy.exe

dphulkar-msft commented 1 month ago

Hi @gsreevastav,

Could you kindly verify the issue using the latest version of AzCopy i,e. 10.26.0 ? The version referenced is quite outdated, and recent updates may have addressed the problem. Testing with the latest version will ensure we're working with the most up-to-date information.

Thank you!