Skip to content

Instantly share code, notes, and snippets.

@rjaeckel
Last active February 20, 2026 22:41
Show Gist options
  • Select an option

  • Save rjaeckel/80334d23a128074a8445cad6afb0e809 to your computer and use it in GitHub Desktop.

Select an option

Save rjaeckel/80334d23a128074a8445cad6afb0e809 to your computer and use it in GitHub Desktop.
PowerShell error handling
# language spec alá openApi:
# https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java
$WUServices = "wuauserv","bits","cryptsvc"
$WUServices | Stop-Service -Name {$_}
<# clear registry and files #>
$WUSerices[-1..-3] | Start-Service { $_ }
Import-Module .\test-module.ps1 -Verbose -Force
$number = 3
$str = "test str"
test-script {
$number += 2
$str
}
""
{
$number = -3
Get-Item "dummy" -ea Stop # -> test-item
[datetime]::Now.Second % 3 -or ( . {
throw "testing error"
}) > $null
} | test-script -Verbose
function test-script {
[cmdletbinding()]param(
[parameter(Mandatory,ValueFromPipeline)][scriptblock]$Commands
)
begin {
[int]$number = 13
}
process {
try {
$number
# add some stuff arround the scriptblock ;-)
. $Commands
$number
& $Commands
$number
}
catch {
#$_.Exception.GetType(),$_.Exception.Message -join ': ' | Write-Error
$_.ErrorStr | Write-Error
<#$_.InvocationInfo|% {
$_.ScriptName,$_.ScriptLineNumber," `t " -join ':'
$_.Line
} |% trim | Write-Verbose#>
$_.ErrorSrc | Write-Verbose
# $_ |% gettype |Write-Verbose
#if ($Debug) {
#$PSItem | gm | Write-Verbose
#$_.Exception
#}
}
finally {
# cleanup sessions and stuff
}
}
end {
$number
}
}
Update-TypeData -TypeName System.Management.Automation.ErrorRecord -MemberName ErrorStr -MemberType ScriptProperty -Value {$this.Exception |% {$_.GetType(),$_.Message -join ': '}} -Force
Update-TypeData -TypeName System.Management.Automation.ErrorRecord -MemberName ErrorSrc -MemberType ScriptProperty -Value {($this.InvocationInfo |% {
$_.ScriptName, $_.ScriptLineNumber -join ':'
$_.Line.Trim()
}) -join " » " } -Force
#
# Modulmanifest für das Modul "TestModule"
#
# Generiert von: r0b
#
# Generiert am: 12.02.2026
#
@{
# RootModule = ''
ModuleVersion = '1.0'
# CompatiblePSEditions = @()
GUID = '9e3dfdb2-0861-4d6d-a265-912fcbd3e067'
Author = 'r0b'
CompanyName = 'out-sourced.net'
Copyright = '(c) 2026 r0b. Alle Rechte vorbehalten.'
Description = 'Module to test scoping'
# PowerShellVersion = ''
# PowerShellHostName = ''
# PowerShellHostVersion = ''
# DotNetFrameworkVersion = ''
# CLRVersion = ''
# ProcessorArchitecture = ''
# RequiredModules = @()
# RequiredAssemblies = @()
ScriptsToProcess = 'test-module.ps1'
# TypesToProcess = @() #(.ps1xml)
# FormatsToProcess = @() #(.ps1xml)
# NestedModules = @()
# FunctionsToExport = @()
CmdletsToExport = 'test-*'
#VariablesToExport = '*'
# AliasesToExport = @()
# DscResourcesToExport = @()
# ModuleList = @()
FileList = 'test-module.ps1'
PrivateData = @{
PSData = @{
# Tags = @()
# LicenseUri = ''
# ProjectUri = ''
# IconUri = ''
# ReleaseNotes = ''
} # End PSData-Hashtabelle
} # End PrivateData-Hashtabelle
# HelpInfo-URI dieses Moduls
# HelpInfoURI = ''
# DefaultCommandPrefix = ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment