ErrorAction Stop by default in PowerShell scripts - powershell

How can I set in a PowerShell script that all the actions have the property ErrorAction set to Stop?
P.S.: Powershell 2.0

Set the $ErrorActionPreference for the script to stop:
$ErrorActionPreference = "Stop"
edit: As ivan-mirchev pointed out, of course this change is not permanet and only active in the runspace where the code is executed. When you open a new Powershell session it will have the default value of $ErrorAction = 'Continue'

Related

powershell executionPolicy message

Set-ExecutionPolicy Bypass -File 'E:\Script-Popup\Trigger-Start-Popup.ps1'
Set-ExecutionPolicy Bypass -File 'E:\Script-Popup\Popup-Message - Start.ps1'
powerShell -sta -file 'E:\Script-Popup\Popup-Message - Start.ps1' -Confrim:$true
The script always stops for me here and I want it to run automatically, I tried -Force I tried -Confrim:$true
What else can be done to have it automatically confirm it?
I don't mind putting echo Y and enter, a really stupid solution but I've already tried most of them..
You can't put Set-ExecutionPolicy into a script (obviously) but you can use -Force on it. That will skip the confirmation prompt.
Do note that depending on the Scope you set for Set-ExecutionPolicy the policy you set might not have an effect on new powershell instances such as the one started in the third line.
Use Get-ExecutionPolicy -List to see which scope is set to what policy.
As an aside, you don't usually need to set -Confirm to $True as that is its default value (most of the time). Setting -Confirm to $true obviously won't skip any confirmation boxes; to do that you'd need to set it to $False instead.

How to run Multiple command in Windows Powershell sequentially like second executes if first is passed?

To execute multiple commands in PowerShell we use ; . But there is a flaw in it it executes all the commands even if one fails in between.
Is there any substitute for && in Windows Powershell ?
But there is a flaw in it it executes all the commands even if one fails in between.
Try setting the appropriate preference variable - it defaults to Continue in powershell.exe:
$ErrorActionPreference = 'Stop'
Some-Command
Some-CommandThatMightFail
Some-OtherCommand
Now, if Some-CommandThatMightFail throws an error, execution of the containing script or function will stop and return to the caller immediately.
See the about_Preference_Variables topic in the documentation on more information about controlling error handling behavior with $ErrorActionPreference
Beware that the $ErrorActionPreference = 'Stop' assignment only affects the current scope - overwriting it will only affect the error action preference in the script/function/scriptblock where that assignment was executed:
PS C:\> $ErrorActionPreference
Continue
PS C:\> &{
$ErrorActionPreference = 'Stop'
# preference only changes inside this scriptblock
$ErrorActionPreference
}
Stop
PS C:\> $ErrorActionPreference # still Continue in parent scope
Continue

Alter behaviour of every cmdlet in Powershell session to pass verbose flag

If I wanted to alter the behaviour of every cmdlet in a Powershell session to pass verbose flag I could achieve this in Powershell v3 with $PSDefaultParameterValues like so
$PSDefaultParameterValues['*:Verbose'] = $true
What would be a possible workaround for Powershell v2?
No, this is a 'new' feature in PowerShell 3.0 and up: about_Parameters_Default_Values

Force ErrorActionPreference when running PowerShell?

We use PowerShell for some of our automated build scripts. Unfortunately, by default, PowerShell continues after an error.
Ordinarily, I can change this behaviour by setting $ErrorActionPreference = Stop.
I can't see a corresponding command line switch for PowerShell.exe, and we (deliberately) run the commands with -noprofile, so I can't put it in there.
How do I do this for a build script?
Put it at the top of the script you're running?
$ErrorActionPreference = 'Stop'
Alternatively, you can also get similar control at the cmdlet level using the ErrorAction parameter.
There doesn't seem to be a way to set:
powershell -erroractionpreference stop ...
The following would work:
powershell -command { $ErrorActionPreference = "stop"; .\test.ps1 } -noprofile
There is of course nothing to stop the script (re)setting ErrorActionPreference.

Custom powershell prompt is clearing $lastexitcode

My powershell profile has a custom powershell prompt that unfortunately causes $lastexitcode values to be lost. For instance, given a powershell script "fail.ps1" with contents "exit 123", when I run the script, $? is $false while $lastexitcode is 0. If I instead run powershell without loading my profile with the custom prompt, after running fail.ps1 then $lastexitcode is 123.
Has anyone seen this problem before? Is there a way to preserve $lastexitcode as the prompt is generated?
I ran into this when using Posh-git, https://github.com/dahlbyk/posh-git, a nice powershell prompt for git.
Issue can be resolved by capturing $LASTEXITCODE at the start of the prompt and restoring it at the end:
function prompt {
$realLASTEXITCODE = $LASTEXITCODE
# ...
$LASTEXITCODE = $realLASTEXITCODE
}
You need to do this to make it work:
function prompt {
$realLASTEXITCODE = $global:LASTEXITCODE
# ...
$global:LASTEXITCODE = $realLASTEXITCODE
# cleanup
Remove-Variable realLASTEXITCODE
}