Skip to content

Instantly share code, notes, and snippets.

@vexx32
Created August 10, 2018 15:42
Show Gist options
  • Save vexx32/57bdfb0e37c176940e94edde12f839e2 to your computer and use it in GitHub Desktop.
Save vexx32/57bdfb0e37c176940e94edde12f839e2 to your computer and use it in GitHub Desktop.
Creates a test module that unconditionally throws an exception during the remove-module call
$Module = New-Module -Name 'ImportMe' -ScriptBlock {
class TryMeException : System.Exception {
TryMeException() : base() {}
TryMeException([string]$Message) : base($Message) {}
}
$onremove = {
throw [TryMeException]::new("HOW DARE YOU STEAL THAT CAR")
}
$ExecutionContext.SessionState.Module.OnRemove += $onremove
}
Import-Module $Module
Remove-Module -Name 'ImportMe'
Get-Module 'ImportMe'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment