|| die || die in perl is something short that I can put wi...">

Is there an equivalent "... || die" in powershell?

It seems like <statement> || die <statement> || die in perl is something short that I can put with every critical statement to avoid powershell bothering with the rest of the script if something goes wrong.

+5
source share
2 answers

Most commands support the general -ErrorAction parameter. Specifying -ErrorAction Stop usually stops the script on error. See Get-Help about_CommonParameters .

By default, -ErrorAction is Continue . You can change the default setting by changing the value of $ErrorActionPreference . See Get-Help about_Preference_Variables .

If verbosity is indeed a problem, -ErrorAction has the alias -ea .

+5
source

Another way to implement the constructor ...|| die ...|| die in PowerShell without the need to add huge try-catch constructs will the $? automatic variable be used $? .
From Get-Help about_Automatic_variables :

 $? Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed. 

Just add the following after each critical statement:

 if(-not $?){ # Call Write-EventLog or write $Error[0] to an xml or txt file if you like Exit(1) } 
+1
source

All Articles