Open gsreevastava opened 4 years ago
@nakulkar-msft are you familiar with Powershell? Could you please take a look?
@zezha-msft - what info you need ?
@zezha-msft I have similar issue. how can we capture azcopy errors?
@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.
@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.
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?
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
}
`
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.
would be helpful to fix this issue of returning proper error code from AZCOPY for PS scripting or automation.
We have used the below approach for Error Handling :
$azcopyPath = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy\AzCopy.exe"
$ErrorActionPreference = "Stop"
try {
& $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.
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.
@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];
}
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
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!
Which version of the AzCopy was used?
Note: The version is visible when running AzCopy without any argument
Which platform are you using? (ex: Windows, Mac, 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.
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
How can we reproduce the problem in the simplest way?
Have you found a mitigation/solution?
No