Chapter 26 – Using Someone else’s script
AnnouncementI have released my new course on Udemy, Kubernetes By Example. Sign up now to get free lifetime access!
for lots of sample powershell scripts, check out this code repository:
Here is the structure of the if-else statement that you can place inside a script:
# Here is an if-elseif-else statement. For some reason this is if-conditions are if-else conditions are not really covered in this book
# but is covered in the 2nd book.
if ($var -gt 2)
Write-Host “The value $var is greater than 2.”
elseif ($var -eq 2)
Write-Host “The value $var is equal to 2.”
Write-Host “The value $var is less than 2 or was not created or initialized.”
In bash, when you want to check whether an ftp/scp transfer has been successful, then you check the exit code ($?) right after
running the ftp/scp command. This check is done by placing the exit code within it’s own if statement, i.e.:
if [ $? -gt 0 ]; then
log “file transfer failed”
archive fail $file
However, in ps, you do the same thing, but with construct called the try-catch. Covered in the next book.