-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSplit-Verbose-Filter-By-Regex.ps1
43 lines (41 loc) · 1.2 KB
/
Split-Verbose-Filter-By-Regex.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<#
from: Justin Grote's gist:
<https://gist.github.com/JustinGrote/c9f2f57fe15b2936c1a41c051309b774>
#>
using namespace System.Management.Automation
using namespace System.Collections.Generic
function Split-Verbose {
<#
.SYNOPSIS
Services a similar purpose to the cmdletbinding WarningVariable parameter but for verbose.
.NOTES
In order for this to work your previous command must have 4>&1 at the end of the command prior to piping to this
#>
[CmdletBinding()]
param(
#Use this to save verbose output to a variable. If not specified then it will continue to be output normal
[string]$VariableName,
#Filter via a regex statement. Verbose records that match this will be retained
[Regex]$Filter,
[Parameter(ValueFromPipeline)]$InputObject
)
begin {
if ($VariableName) {
$var = Set-Variable -Name $VariableName -Scope 1 -Value ([List[VerboseRecord]]::new()) -PassThru
}
}
process {
if ($InputObject -is [VerboseRecord]) {
if ($Filter -and -not $Filter.IsMatch($InputObject)) {
return
}
if ($VariableName) {
$var.Value.Add($InputObject)
return
}
Write-Verbose $InputObject
return
}
$InputObject
}
}