How it works...
Note that the first command given to us is a native Linux command; the second is a PowerShell command.
We use two automatic variables here to find out whether there was an error in execution. The first is $?, which checks if the last command executed without an error. The value the variable holds is Boolean, that is, TRUE, standing for successful execution. In our script, we negate this in order to use it with the if block.
The other variable is $LASTEXITCODE, which contains the exit code of the last command. In our case, the exit code was 2. The ping: ghostname: Name or service not known error is also displayed.
In the second command, we use -ErrorAction SilentlyContinue to hide errors at the terminal. However, these errors ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access