Test-Error.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<#
 
.SYNOPSIS
Recurses an error record or exception object and returns true/false if a match is found.
 
.DESCRIPTION
Loops through information caught in catch blocks; from an ErrorRecord (and its InvocationInfo), to Exception, and InnerException.
 
These are then compared to a specific type, or, a hash table with a set of desired properties and settings.
 
.PARAMETER ErrorRecord
An error record or exception. By default the last error is used.
 
.PARAMETER Type
A type to compare against. If any item in the expanded ErrorRecord entry matches this typethen a $true result is returned.
 
.PARAMETER Property
A hash table with a series of Name/Value property pairs. If all of these exist on any object in the expanded ErrorRecord entry then a $true result is returned.
 
.OUTPUTS
$true or $false.
 
.EXAMPLE
Test-Error Microsoft.SqlServer.Management.Sdk.Sfc.InvalidVersionEnumeratorException
 
Tests whether the ErrorRecord, Exception, InnerException, and so forth are this specific type of Exception. When providing this do not put it into string quotes.
 
.EXAMPLE
Test-Error @{ Number = 954; Class = 14; State = 1; }
 
Tests whether the ErrorRecord, Exception, InnerException, and so forth have an item with all 3 properties which match these conditions.
 
#>


function Test-Error {
    [CmdletBinding(DefaultParameterSetName = "Type")]
    [OutputType("System.Boolean")]
    param (
        [Parameter(ValueFromPipeline = $true, ParameterSetName = "Type")]
        [Parameter(ValueFromPipeline = $true, ParameterSetName = "TypeName")]
        [Parameter(ValueFromPipeline = $true, ParameterSetName = "Property")]
        $ErrorRecord = $null,

        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "Type")]
        [type] $Type,
        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "TypeName")]
        [string] $TypeName,
        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "Property")]
        [hashtable] $Property
    )

    if (!$ErrorRecord) {
        $ErrorRecord = (Get-Variable -Name Error -Scope 2).Value | Select-Object -First 1
    }

    $expandedErrorRecord = Resolve-Error $ErrorRecord

    switch ($PSCmdlet.ParameterSetName) {
        "Type" {
            if ($expandedErrorRecord | Where-Object { $_ -is $Type }) {
                return $true
            }
            break
        }

        "TypeName" {
            if ($expandedErrorRecord | Where-Object { @($_.GetType(), $_.GetType().FullName) -contains $TypeName }) {
                return $true
            }
            break
        }

        "Property" {
            foreach ($record in $expandedErrorRecord) {
                $match = $true

                foreach ($thisProperty in $Property.GetEnumerator()) {
                    if (!$record.psobject.Properties[$thisProperty.Key] -or !$record.$($thisProperty.Key) -or $record.$($thisProperty.Key).ToString() -ne $thisProperty.Value) {
                        $match = $false
                        break
                    }
                }

                if ($match) {
                    return $true
                }
            }
            break
        }
    }

    return $false
}