forked from dsccommunity/UpdateServicesDsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateServicesHelper.psm1
More file actions
85 lines (64 loc) · 2.27 KB
/
UpdateServicesHelper.psm1
File metadata and controls
85 lines (64 loc) · 2.27 KB
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
# Set Global Module Verbose
$VerbosePreference = 'Continue'
# Load Localization Data
Import-LocalizedData LocalizedData -filename UpdateServices.strings.psd1 -ErrorAction SilentlyContinue
Import-LocalizedData USLocalizedData -filename UpdateServices.strings.psd1 -UICulture en-US -ErrorAction SilentlyContinue
<#
.SYNOPSIS
Simplifies writing a terminating error
.PARAMETER ErrorType
A descriptive value that specifies the type of error
.PARAMETER FormatArgs
Optional arguments to specify formatting of the error
.PARAMETER ErrorCategory
Optional value to set the error category
.PARAMETER TargetObject
The object that was being processed when the error occurred
#>
function New-TerminatingError
{
[CmdletBinding()]
[OutputType([System.Management.Automation.ErrorRecord])]
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$ErrorType,
[parameter(Mandatory = $false)]
[String[]]
$FormatArgs,
[parameter(Mandatory = $false)]
[System.Management.Automation.ErrorCategory]
$ErrorCategory = [System.Management.Automation.ErrorCategory]::OperationStopped,
[parameter(Mandatory = $false)]
[Object]
$TargetObject = $null
)
$errorMessage = $LocalizedData.$ErrorType
if(!$errorMessage)
{
$errorMessage = ($LocalizedData.NoKeyFound -f $ErrorType)
if(!$errorMessage)
{
$errorMessage = ("No Localization key found for key: {0}" -f $ErrorType)
}
}
$errorMessage = ($errorMessage -f $FormatArgs)
$callStack = Get-PSCallStack
# Get Name of calling script
if($callStack[1] -and $callStack[1].ScriptName)
{
$scriptPath = $callStack[1].ScriptName
$callingScriptName = $scriptPath.Split('\')[-1].Split('.')[0]
$errorId = "$callingScriptName.$ErrorType"
}
else
{
$errorId = $ErrorType
}
Write-Verbose -Message "$($USLocalizedData.$ErrorType -f $FormatArgs) | ErrorType: $errorId"
$exception = New-Object System.Exception $errorMessage;
$errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $errorId, $ErrorCategory, $TargetObject
return $errorRecord
}