Skip to content

Instantly share code, notes, and snippets.

@dezren39
Last active November 17, 2023 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dezren39/814da39a489b22433edbad899b753bd1 to your computer and use it in GitHub Desktop.
Save dezren39/814da39a489b22433edbad899b753bd1 to your computer and use it in GitHub Desktop.
Set-StrictMode -Version Latest
$PSNativeCommandUseErrorActionPreference = $true
if ($PSNativeCommandUseErrorActionPreference) {
# always true, this is a linter workaround
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
# sometimes you might need to update these to unicode or another format.
# unicode is the only normal one that stays the same between powershell 5 and pwsh core.
# utf8 may or may not have a bom.
# utf8nobom doesn't exist in powershell
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
$utf8 = New-Object System.Text.UTF8Encoding($true)
[Console]::OutputEncoding = $utf8
$OutputEncoding = $utf8
}
function CheckLastExitCode {
param ([int[]]$SuccessCodes = @(0))
if (!$?) {
if (-not (Test-Path variable://LastExitCode)) {
$LastExitCode = $LastExitCode
}
Write-Host "Last CMD failed $LastExitCode"
exit
}
if (-not (Test-Path variable://LastExitCode)) {
$LastExitCode = 0
}
if ($SuccessCodes -notcontains $LastExitCode) {
Write-Host "EXE RETURNED EXIT CODE $LastExitCode"
exit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment