This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
CommonResourceHelper.psm1
187 lines (160 loc) · 4.78 KB
/
CommonResourceHelper.psm1
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<#
.SYNOPSIS
Tests if the current machine is a Nano server.
#>
function Test-IsNanoServer
{
[OutputType([System.Boolean])]
[CmdletBinding()]
param ()
$serverLevelsRegKey = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels'
if (Test-Path -Path $serverLevelsRegKey)
{
$serverLevels = Get-ItemProperty -Path $serverLevelsRegKey
if ($serverLevels.NanoServer -eq 1)
{
$isNanoServer = $true
}
else
{
$isNanoServer = $false
}
}
else
{
$isNanoServer = $false
}
return $isNanoServer
}
<#
.SYNOPSIS
Tests whether or not the command with the specified name exists.
.PARAMETER Name
The name of the command to test for.
#>
function Test-CommandExists
{
[OutputType([System.Boolean])]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name
)
$command = Get-Command -Name $Name -ErrorAction 'SilentlyContinue'
return ($null -ne $command)
}
<#
.SYNOPSIS
Creates and throws an invalid argument exception.
.PARAMETER Message
The message explaining why this error is being thrown.
.PARAMETER ArgumentName
The name of the invalid argument that is causing this error to be thrown.
#>
function New-InvalidArgumentException
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Message,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$ArgumentName
)
$argumentException = New-Object -TypeName 'ArgumentException' `
-ArgumentList @($Message, $ArgumentName)
$newObjectParams = @{
TypeName = 'System.Management.Automation.ErrorRecord'
ArgumentList = @($argumentException, $ArgumentName, 'InvalidArgument', $null)
}
$errorRecord = New-Object @newObjectParams
throw $errorRecord
}
<#
.SYNOPSIS
Creates and throws an invalid operation exception.
.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.
#>
function New-InvalidOperationException
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Message,
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.ErrorRecord]
$ErrorRecord
)
if ($null -eq $Message)
{
$invalidOperationException = New-Object -TypeName 'InvalidOperationException'
}
elseif ($null -eq $ErrorRecord)
{
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' `
-ArgumentList @( $Message )
}
else
{
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' `
-ArgumentList @( $Message, $ErrorRecord.Exception )
}
$newObjectParams = @{
TypeName = 'System.Management.Automation.ErrorRecord'
ArgumentList = @( $invalidOperationException.ToString(), 'MachineStateIncorrect', 'InvalidOperation', $null )
}
$errorRecordToThrow = New-Object @newObjectParams
throw $errorRecordToThrow
}
<#
.SYNOPSIS
Retrieves the localized string data based on the machine's culture.
Falls back to en-US strings if the machine's culture is not supported.
.PARAMETER ResourceName
The name of the resource as it appears before '.strings.psd1' of the localized
string file.
For example:
For WindowsOptionalFeature: MSFT_WindowsOptionalFeature
For Service: MSFT_ServiceResource
For Registry: MSFT_RegistryResource
#>
function Get-LocalizedData
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$ResourceName
)
$resourceDirectory = Join-Path -Path $PSScriptRoot -ChildPath $ResourceName
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath $PSUICulture
if (-not (Test-Path -Path $localizedStringFileLocation))
{
# Fallback to en-US
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath 'en-US'
}
Import-LocalizedData `
-BindingVariable 'localizedData' `
-FileName "$ResourceName.strings.psd1" `
-BaseDirectory $localizedStringFileLocation
return $localizedData
}
Export-ModuleMember -Function @( 'Test-IsNanoServer', 'New-InvalidArgumentException',
'New-InvalidOperationException', 'Get-LocalizedData' )