Skip to content

Instantly share code, notes, and snippets.

@jhoneill
Created June 13, 2022 20:59
Show Gist options
  • Save jhoneill/7239a13c0faaa2b875c267ea093464dd to your computer and use it in GitHub Desktop.
Save jhoneill/7239a13c0faaa2b875c267ea093464dd to your computer and use it in GitHub Desktop.
function one {[cmdletbinding()]param()
begin {Write-Host "one begins"} end {Write-host "one ends"}
process {1,2,3}
}
function two {param ([parameter(ValueFromPipeline)]$p)
begin {Write-Host "two begins"} end {Write-host "two ends"}
process {
if ($p -eq 2) {$p / 0 } # cause a division by zero error
$p
}
}
function three {param ([parameter(ValueFromPipeline)]$p)
begin {Write-Host "three begins"} end {Write-host "three ends"}
process {
Write-host "three Processed $p"
$p
}
}
function four {[cmdletbinding()]param()
begin {Write-Host "four begins"} end {Write-host "four ends"}
process {
$result = one | two | three
Write-host "And the result is "
$result
}
}
four
function four {[cmdletbinding()]param()
begin {Write-Host "four begins"} end {Write-host "four ends"}
process { # Add try {} Catch {}
try { $result = one | two | three} catch {Write-Host "Error in the pipeline"}
Write-host "And the result is "
$result
}
}
four
try { one | two | three} catch {Write-Host "Error in the pipeline"}
function five {param ($p) two $P}
function six {param ($p) five $P}
try {six 2} catch {"error happened"}
function four {[cmdletbinding()]param()
begin {Write-Host "four begins"} end {Write-host "four ends"}
process { #remove try {} / catch and use -ErrorAction Stop
Write-Host "Error action preference in four is $errorActionPreference"
$result = one | two -ErrorAction Stop | three
Write-host "And the result is "
$result
}
}
four
four -ErrorAction SilentlyContinue
four -ErrorAction Stop
try {2/0} catch {"Bad calculation"}
try {Start-Sleep ([timespan]::FromDays(30)) } catch {"Bad timespan"} # not in 7.2.x or earlier
try {Get-Item "Nonexistent" } catch {"Bad item"}
function two {param ([parameter(ValueFromPipeline)]$p )
begin {Write-Host "two begins"} end {Write-host "two ends"}
process { # change the cause of the error
if ($p -eq 2) {Get-Item "NonExistent" -ErrorAction Stop}
$p
}
}
function four {[cmdletbinding()]param()
begin {Write-Host "four begins"} end {Write-host "four ends"}
process { # back to no error action or try/catch
Write-host "Error action preference in four is $ErrorActionPreference"
$result = one | two | three
Write-host "And the result is "
$result
}
}
four
function two {param ([parameter(ValueFromPipeline)]$p )
begin {Write-Host "two begins"} end {Write-host "two ends"}
process { # remove -ErrorAction stop
if ($p -eq 2) {Get-Item "NonExistent"}
$p
}
}
function four {[cmdletbinding()]param()
begin {Write-Host "four begins"} end {Write-host "four ends"}
process { # use -ErrorAction Stop
Write-host "Error action preference in four is $ErrorActionPreference"
$result = one | two -ErrorAction Stop | three
Write-host "And the result is "
$result
}
}
four
function seven {[cmdletbinding()]param()
begin {Write-Host "seven begins"} end {Write-host "seven ends"}
process { # create an error record, which contains an exception, then throw it.
$ex = [System.Exception]::new("This is not the result you are looking for")
$er = [System.Management.Automation.ErrorRecord]::new(
$ex, "42", [System.Management.Automation.ErrorCategory]::OperationStopped, $null)
$PSCmdlet.ThrowTerminatingError($er)
}
}
function four {[cmdletbinding()]param()
begin {Write-Host "four begins"} end {Write-host "four ends"}
process { # back to no error action or try/catch
Write-host "Error action preference in four is $ErrorActionPreference"
$result = one | two | three
Write-host "And the result is "
$result
}
}
function two { param ([parameter(ValueFromPipeline)]$p )
begin {Write-Host "two begins"} end {Write-host "two ends"}
process {
if ($P -eq 2) {seven}
$p
}
}
four
function two { param( [parameter(ValueFromPipeline)]$p )
begin {Write-Host "two begins"} end {Write-host "two ends"}
process {
if ($p -eq 2) {
$ex = [System.Exception]::new("A different error")
$er = [System.Management.Automation.ErrorRecord]::new($ex, "42",
[System.Management.Automation.ErrorCategory]::OperationStopped, $null)
$PSCmdlet.ThrowTerminatingError($er)
}
$p
}
}
four
function two { param( [parameter(ValueFromPipeline)]$p )
begin {Write-Host "two begins"} end {Write-host "two ends"}
process {
if ($p -eq 2) {
$ex = [System.Exception]::new("A different error")
$er = [System.Management.Automation.ErrorRecord]::new($ex, "42",
[System.Management.Automation.ErrorCategory]::OperationStopped, $null)
throw $er
}
$p
}
}
fourr
four
function five {[cmdletbinding()] param ($p)
Write-host "Error action preference in five is $ErrorActionPreference"
Two $P -ErrorAction continue ;
"Five done"
}
five 2
five 2 -ErrorAction SilentlyContinue
$x = one | two -ErrorAction SilentlyContinue | three
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::break
get-item 'nonexistent'
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Inquire
get-item 'nonexistent'
get-item 'nonexistent' -erroraction stop
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::break
get-item 'nonexistent' -errorAction stop
q
q
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
throw
pwsh.exe -command 'echo "Hello world"'
pwsh.exe -command "echo 'Hello world'"
Get-Unique -?
Get-Command Get-Unique
@{ 42 = 'foo1' }.42
[pscustomobject]@{ 42 = 'foo1' }
$psco = [pscustomobject]@{ 42 = 'foo1' }
$Psco.42
$h = @{ 42 = 'foo1' }
$h.42
$h = (@{ "42" = 'foo1' })
$h.42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment