AzCopy: How to know if the copy was success or not - powershell

I have a script in Ubuntu that copy only one file each hour to the Storage Account. I am using azcopy filename.tar https://<storage>.blob.core.windows.net/<container>.
This script is working but I'd like to check if the copy was success or not, for example:
validcopy = azcopy copy filename.tar https://<storage>.blob.core.windows.net/<container>
if(validcopy){
echo "Success"
} else {
echo "Failure"
}
Also, I tried using Power Shell in linux (pwsh), but unsuccess.
Please, can someone help me?

I got a alternative solution for this issue.
I used exit code in bash shell. Every Linux or Unix command executed by the shell script or user has an exit status. Exit status is an integer number. 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was a failure.
A particular shell variable called $? to get the exit status of the previously executed command
It was like this:
azcopy copy filename.tar https://<storage>.blob.core.windows.net/<container>
if [[ $? -gt 0 ]]
then
echo "Failure"
else
echo "Success"
fi

Related

Powershell exitcode from MDM

I'm writing a powershell script that:
Downloads a file
Checks it's hashsum
If hashsum is correct it installs it
If hashsum is incorrect it does not install it
I have a problem, where I'm using an MDM to deploy it automatically on many machines at the same time. Feedback from MDM shows that installation failed - but installed file works as intended for sure. Therefore most likely MDM receives bad return value. MDM exits code are like this
Success exit code equal 0
Failed exit code not equal 0
So: How to make sure powershell script return Success after 3. If hashsum is correct it installs it
Will $LASTEXITCODE = 0 make it work?
Just use exit and the exit code you want. Like:
Function CustomExit{
Write-Output "This function returns exit code 1613"
Exit 1613 # Custom Exit Code
}
CustomExit
to test just run the file and give out the exitcode
PS C:\script> .\CustomExitCode.ps1
This function returns exit code 1613
PS C:\script> $LASTEXITCODE
1613
PS C:\script>

How to return error code 1 on error from PowerShell?

Can you give an example how to return error code 1 on error using PowerShell?
For example handling this situation:
if ($serviceUserName) {
cmd /c $serviceBinaryFinalPath install -username:$serviceUserName -password:$serviceUserPassword -servicename:"$ServiceName" -displayname:"$serviceDisplayName" -description:"$serviceDescription"
} else {
cmd /c $serviceBinaryFinalPath install --localsystem -servicename:"$ServiceName" -displayname:"$serviceDisplayName" -description:"$serviceDescription"
}
You're running external commands there, so you need to check the automatic variable $LastExitCode for detecting errors:
if ($LastExitCode -ne 0) {
exit 1
}
Or just exit with the exit code of the last external command:
exit $LastExitCode
Note that some external commands (robocopy for instance) use exit codes not only for signaling errors, but also for providing non-error status information.

checking the status of command executed successfully

Sorry I am a newbie in PowerShell. I am running a script which executes some commands to copy files from a system.. Is there a way to capture the status of the command whether it completed successfully before moving to the next command?
For example:
$scp_cmd = "scp.exe -a root#192.168.1.1:/files/repo1 /repo1"
$rsync_cmd = "rsync.exe -tvP root#192.168.1.2:/files/repo2 /repo2"
Invoke-Expression $scp_cmd
# How do we check whether the command completed successfully?
Invoke-Expression $rsync_cmd
# How do we check whether the command completed successfully?
Use the $LASTEXITCODE automatic variable. It works both with Invoke-Expression and the & call operator:
$rsync_cmd = "rsync.exe -tvP root#192.168.1.2:/files/repo2 /repo2"
Invoke-Expression $rsync_cmd
if($LASTEXITCODE)
{
# exit code from rsync was not 0
}

Capistrano run local command exit on failure

I would like to run local commands and exit on failure of any command. whats the best way to do this with capistrano? run_locally will continue going on failure.
Do i have to check the last commands exist status everytime (or create a custom run locally function)?
I had to create my own function like this:
task :build_backend do
run_local("echo hello")
run_local("abcdef")
run_local("echo 'not run'")
end
def run_local(cmd)
system cmd
if($?.exitstatus != 0) then
puts 'exit code: ' + $?.exitstatus.to_s
exit
end
end
Using this
Generally in shell you can run multiple commands the way you want by command1 --some-argument foo && command2 && command3. the && operator will cause, that the chain will stop when one command fails (returns non-zero return value).

Returning an exit code from a shell script that was called from inside a perl script

I have a perl script (verifyCopy.pl) that uses system() to call a shell script (intercp.sh).
From inside the shell script, I have set up several exit's with specific exit codes and I'd like to be able to do different things based on which exit code is returned.
I've tried using $?, I have tried assigning the value of system("./intercp.sh") to a variable then checking the value of that, but the error message is always 0.
Is this because even though something inside the shell script fails, the actual script succeeds in running?
I tried adding a trap in the shell script (ie trap testexit EXIT and testexit() { exit 222; } but that didn't work either.
$? should catch the exit code from your shell script.
$ cat /tmp/test.sh
#!/bin/sh
exit 2
$ perl -E 'system("/tmp/test.sh"); say $?'
512
Remember that $? is encoded in the traditional manner, so $? >> 8 gives the exit code, $? & 0x7F gives the signal, and $? & 0x80 is true if core was dumped. See perlvar for details.
Your problem may be one of several things: maybe your shell script isn't actually exiting with the exit code (maybe you want set -e); maybe you have a signal handle for SIGCHLD eating the exit code; etc. Try testing with the extremely simple shell script above to see if its a problem in your perl script or your shell script.