-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNew-ObjectNotFoundException.ps1
74 lines (62 loc) · 1.81 KB
/
New-ObjectNotFoundException.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
<#
.SYNOPSIS
Creates and throws an object not found exception.
.DESCRIPTION
Creates and throws an object not found exception.
.OUTPUTS
None
.PARAMETER Message
The message explaining why this error is being thrown.
.PARAMETER ErrorRecord
The error record containing the exception that is causing this terminating error.
.OUTPUTS
None
.EXAMPLE
try
{
Get-ChildItem -Path $path
}
catch
{
New-ObjectNotFoundException -Message 'Could not get files' -ErrorRecord $_
}
Creates and throws an object not found exception with the message 'Could not
get files' and includes the exception that caused this terminating error.
#>
function New-ObjectNotFoundException
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Message,
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.ErrorRecord]
$ErrorRecord
)
if ($null -eq $ErrorRecord)
{
$exception = New-Object -TypeName 'System.Exception' `
-ArgumentList @($Message)
}
else
{
$exception = New-Object -TypeName 'System.Exception' `
-ArgumentList @($Message, $ErrorRecord.Exception)
}
$newObjectParameters = @{
TypeName = 'System.Management.Automation.ErrorRecord'
ArgumentList = @(
$exception.ToString(),
'MachineStateIncorrect',
'ObjectNotFound',
$null
)
}
$errorRecordToThrow = New-Object @newObjectParameters
throw $errorRecordToThrow
}