Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active November 10, 2023 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/853492171218817ccaaa9fea9213c133 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/853492171218817ccaaa9fea9213c133 to your computer and use it in GitHub Desktop.
An alternate PowerShell history command to let you filter on executionstatus.
#requires -version 5.1
<#
This is a copy of:
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-History 3.0.0.0 Microsoft.PowerShell.Core
Created: 07 July 2020
Author : Jeff Hicks
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
#>
Function Get-PSHistory {
[CmdletBinding(DefaultParameterSetName = "ID")]
[alias("hh")]
[OutputType([Microsoft.PowerShell.Commands.HistoryInfo])]
Param(
[Parameter(Position=0, ValueFromPipeline=$true,ParameterSetName="ID")]
[ValidateRange(1, 9223372036854775807)]
[long[]]$Id,
[Parameter(Position=1,ParameterSetName="ID")]
[Parameter(ParameterSetName="status")]
[ValidateRange(0, 32767)]
[int]$Count,
[Parameter(ParameterSetName="status",HelpMessage="Filter by execution state")]
[Alias("es")]
[System.Management.Automation.Runspaces.PipelineState]$ExecutionState
)
Begin {
Write-Verbose "[BEGIN ] Starting $($MyInvocation.Mycommand)"
Write-Verbose "[BEGIN ] Using parameter set $($PSCmdlet.ParameterSetName)"
if ($PSBoundParameters.ContainsKey("ExecutionState")) {
[void]($PSBoundParameters.Remove("ExecutionState"))
#also remove Count because that will be used for a slightly different purpose
[void]($PSBoundParameters.Remove("Count"))
}
Write-Verbose ($PSBoundParameters | Out-String)
} #begin
Process {
if ($ExecutionState) {
Write-Verbose "[PROCESS] Filtering on ExecutionState"
$filtered = (Get-History @PSBoundParameters).where({$_.ExecutionStatus -eq $executionState})
if ($count -gt 0) {
Write-Verbose "[PROCESS] Getting $count most recent entries"
$filtered | Select-Object -last $count
}
else {
$Filtered
}
}
else {
Get-History @PSBoundParameters
}
} #process
End {
Write-Verbose "[END ] Ending $($MyInvocation.Mycommand)"
} #end
} #end function Get-PSHistory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment