This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathMSFT_WindowsFeature.psm1
More file actions
592 lines (493 loc) · 18.4 KB
/
MSFT_WindowsFeature.psm1
File metadata and controls
592 lines (493 loc) · 18.4 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# Global needed to indicate if a restart is required
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
param ()
$errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'
Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) `
-ChildPath 'CommonResourceHelper.psm1')
# Localized messages for verbose and error messages in this resource
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_WindowsFeature'
<#
.SYNOPSIS
Retrieves the status of the role or feature with the given name on the target machine.
.PARAMETER Name
The name of the role or feature to retrieve
.PARAMETER Credential
The credential (if required) to retrieve the role or feature.
Optional.
.NOTES
If the specified role or feature does not contain any subfeatures then
IncludeAllSubFeature will be set to $false. If the specified feature contains one
or more subfeatures then IncludeAllSubFeature will be set to $true only if all the
subfeatures are installed. Otherwise, IncludeAllSubFeature will be set to $false.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential
)
Write-Verbose -Message ($script:localizedData.GetTargetResourceStartMessage -f $Name)
Import-ServerManager
Write-Verbose -Message ($script:localizedData.QueryFeature -f $Name)
$isWinServer2008R2SP1 = Test-IsWinServer2008R2SP1
if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
$feature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $Name } `
-ComputerName . `
-Credential $Credential
}
else
{
$feature = Get-WindowsFeature @PSBoundParameters
}
Assert-SingleInstanceOfFeature -Feature $feature -Name $Name
$includeAllSubFeature = $true
if ($feature.SubFeatures.Count -eq 0)
{
$includeAllSubFeature = $false
}
else
{
foreach ($currentSubFeatureName in $feature.SubFeatures)
{
$getWindowsFeatureParameters = @{
Name = $currentSubFeatureName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$getWindowsFeatureParameters['Credential'] = $Credential
}
if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
<#
Calling Get-WindowsFeature through Invoke-Command to start a new process with
the given credential since Get-WindowsFeature doesn't support the Credential
attribute on this server.
#>
$subFeature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $currentSubFeatureName } `
-ComputerName . `
-Credential $Credential
}
else
{
$subFeature = Get-WindowsFeature @getWindowsFeatureParameters
}
Assert-SingleInstanceOfFeature -Feature $subFeature -Name $currentSubFeatureName
if (-not $subFeature.Installed)
{
$includeAllSubFeature = $false
break
}
}
}
if ($feature.Installed)
{
$ensureResult = 'Present'
}
else
{
$ensureResult = 'Absent'
}
Write-Verbose -Message ($script:localizedData.GetTargetResourceEndMessage -f $Name)
# Add all feature properties to the hash table
return @{
Name = $Name
DisplayName = $feature.DisplayName
Ensure = $ensureResult
IncludeAllSubFeature = $includeAllSubFeature
}
}
<#
.SYNOPSIS
Installs or uninstalls the role or feature with the given name on the target machine
with the option of installing or uninstalling all subfeatures as well.
.PARAMETER Name
The name of the role or feature to install or uninstall.
.PARAMETER Ensure
Specifies whether the role or feature should be installed ('Present')
or uninstalled ('Absent').
By default this is set to Present.
.PARAMETER Source
Specifies a path to additional feature source files if necessary for the installation
of the stated roles or features.
.PARAMETER IncludeAllSubFeature
Specifies whether or not all subfeatures should be installed or uninstalled with
the specified role or feature. Default is false.
If this property is true and Ensure is set to Present, all subfeatures will be installed.
If this property is false and Ensure is set to Present, subfeatures will not be installed or uninstalled.
If Ensure is set to Absent, all subfeatures will be uninstalled.
.PARAMETER Credential
The credential (if required) to install or uninstall the role or feature.
Optional.
.PARAMETER LogPath
The custom path to the log file to log this operation.
If not passed in, the default log path will be used (%windir%\logs\ServerManager.log).
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,
[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Source,
[Parameter()]
[System.Boolean]
$IncludeAllSubFeature = $false,
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$LogPath
)
Write-Verbose -Message ($script:localizedData.SetTargetResourceStartMessage -f $Name)
Import-ServerManager
$isWinServer2008R2SP1 = Test-IsWinServer2008R2SP1
if ($Ensure -eq 'Present')
{
$addWindowsFeatureParameters = @{
Name = $Name
IncludeAllSubFeature = $IncludeAllSubFeature
}
if ($PSBoundParameters.ContainsKey('LogPath'))
{
$addWindowsFeatureParameters['LogPath'] = $LogPath
}
Write-Verbose -Message ($script:localizedData.InstallFeature -f $Name)
if (($isWinServer2008R2SP1 -eq $false) -and $PSBoundParameters.ContainsKey('Source'))
{
$addWindowsFeatureParameters['Source'] = $Source
}
if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
<#
Calling Add-WindowsFeature through Invoke-Command to start a new process with
the given credential since Add-WindowsFeature doesn't support the Credential
attribute on this server.
#>
$feature = Invoke-Command -ScriptBlock { Add-WindowsFeature @addWindowsFeatureParameters } `
-ComputerName . `
-Credential $Credential
}
else
{
if ($PSBoundParameters.ContainsKey('Credential'))
{
$addWindowsFeatureParameters['Credential'] = $Credential
}
$feature = Add-WindowsFeature @addWindowsFeatureParameters
}
if ($null -ne $feature -and $feature.Success)
{
Write-Verbose -Message ($script:localizedData.InstallSuccess -f $Name)
# Check if reboot is required, if so notify the Local Configuration Manager.
if ($feature.RestartNeeded -eq 'Yes')
{
Write-Verbose -Message $script:localizedData.RestartNeeded
$global:DSCMachineStatus = 1
}
}
else
{
New-InvalidOperationException -Message ($script:localizedData.FeatureInstallationFailureError -f $Name)
}
}
# Ensure = 'Absent'
else
{
$removeWindowsFeatureParameters = @{
Name = $Name
}
if ($PSBoundParameters.ContainsKey('LogPath'))
{
$removeWindowsFeatureParameters['LogPath'] = $LogPath
}
Write-Verbose -Message ($script:localizedData.UninstallFeature -f $Name)
if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
<#
Calling Remove-WindowsFeature through Invoke-Command to start a new process with
the given credential since Remove-WindowsFeature doesn't support the Credential
attribute on this server.
#>
$feature = Invoke-Command -ScriptBlock { Remove-WindowsFeature @removeWindowsFeatureParameters } `
-ComputerName . `
-Credential $Credential
}
else
{
if ($PSBoundParameters.ContainsKey('Credential'))
{
$addWindowsFeatureParameters['Credential'] = $Credential
}
$feature = Remove-WindowsFeature @removeWindowsFeatureParameters
}
if ($null -ne $feature -and $feature.Success)
{
Write-Verbose ($script:localizedData.UninstallSuccess -f $Name)
# Check if reboot is required, if so notify the Local Configuration Manager.
if ($feature.RestartNeeded -eq 'Yes')
{
Write-Verbose -Message $script:localizedData.RestartNeeded
$global:DSCMachineStatus = 1
}
}
else
{
New-InvalidOperationException -Message ($script:localizedData.FeatureUninstallationFailureError -f $Name)
}
}
Write-Verbose -Message ($script:localizedData.SetTargetResourceEndMessage -f $Name)
}
<#
.SYNOPSIS
Tests if the role or feature with the given name is in the desired state.
.PARAMETER Name
The name of the role or feature to test the state of.
.PARAMETER Ensure
Specifies whether the role or feature should be installed ('Present')
or uninstalled ('Absent').
By default this is set to Present.
.PARAMETER Source
Specifies a path to additional feature source files if necessary for the installation
of the stated roles or features.
Not used in Test-TargetResource.
.PARAMETER IncludeAllSubFeature
Specifies whether or not the installation state of all subfeatures should be tested with
the specified role or feature. Default is false.
-If this property is set to true and Ensure is set to Present,
each subfeature will be tested to ensure it is installed.
-If this property is set to true and Ensure is set to Absent,
each subfeature will be tested to ensure it is uninstalled.
-If this property is false, subfeatures will not be tested.
.PARAMETER Credential
The Credential (if required) to test the status of the role or feature.
Optional.
.PARAMETER LogPath
The path to the log file to log this operation.
Not used in Test-TargetResource.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,
[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Source,
[Parameter()]
[System.Boolean]
$IncludeAllSubFeature = $false,
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$LogPath
)
Write-Verbose -Message ($script:localizedData.TestTargetResourceStartMessage -f $Name)
Import-ServerManager
$testTargetResourceResult = $false
$getWindowsFeatureParameters = @{
Name = $Name
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$getWindowsFeatureParameters['Credential'] = $Credential
}
Write-Verbose -Message ($script:localizedData.QueryFeature -f $Name)
$isWinServer2008R2SP1 = Test-IsWinServer2008R2SP1
if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
<#
Calling Get-WindowsFeature through Invoke-Command to start a new process with
the given credential since Get-WindowsFeature doesn't support the Credential
attribute on this server.
#>
$feature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $Name } `
-ComputerName . `
-Credential $Credential
}
else
{
$feature = Get-WindowsFeature @getWindowsFeatureParameters
}
Assert-SingleInstanceOfFeature -Feature $feature -Name $Name
# Check if the feature is in the requested Ensure state.
if (($Ensure -eq 'Present' -and $feature.Installed -eq $true) -or `
($Ensure -eq 'Absent' -and $feature.Installed -eq $false))
{
$testTargetResourceResult = $true
if ($IncludeAllSubFeature)
{
# Check if each subfeature is in the requested state.
foreach ($currentSubFeatureName in $feature.SubFeatures)
{
$getWindowsFeatureParameters['Name'] = $currentSubFeatureName
if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
<#
Calling Get-WindowsFeature through Invoke-Command to start a new process with
the given credential since Get-WindowsFeature doesn't support the Credential
attribute on this server.
#>
$subFeature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $currentSubFeatureName } `
-ComputerName . `
-Credential $Credential
}
else
{
$subFeature = Get-WindowsFeature @getWindowsFeatureParameters
}
Assert-SingleInstanceOfFeature -Feature $subFeature -Name $currentSubFeatureName
if (-not $subFeature.Installed -and $Ensure -eq 'Present')
{
$testTargetResourceResult = $false
break
}
if ($subFeature.Installed -and $Ensure -eq 'Absent')
{
$testTargetResourceResult = $false
break
}
}
}
}
else
{
# Ensure is not in the correct state
$testTargetResourceResult = $false
}
Write-Verbose -Message ($script:localizedData.TestTargetResourceEndMessage -f $Name)
return $testTargetResourceResult
}
<#
.SYNOPSIS
Asserts that a single instance of the given role or feature exists.
.PARAMETER Feature
The role or feature object to check.
.PARAMETER Name
The name of the role or feature to include in any error messages that are thrown.
(Not used to assert validity of the feature).
#>
function Assert-SingleInstanceOfFeature
{
[CmdletBinding()]
param
(
[Parameter()]
[System.Management.Automation.PSObject]
$Feature,
[Parameter()]
[System.String]
$Name
)
if ($null -eq $Feature)
{
New-InvalidOperationException -Message ($script:localizedData.FeatureNotFoundError -f $Name)
}
if ($Feature -is [Array] -and $Feature.Count -gt 1)
{
New-InvalidOperationException -Message ($script:localizedData.MultipleFeatureInstancesError -f $Name)
}
}
<#
.SYNOPSIS
Sets up the ServerManager module on the target node.
Throws an error if not on a machine running Windows Server.
#>
function Import-ServerManager
{
param
()
<#
Enable ServerManager-PSH-Cmdlets feature if OS is WS2008R2 Core.
Datacenter = 12, Standard = 13, Enterprise = 14
#>
$serverCoreOSCodes = @( 12, 13, 14 )
$operatingSystem = Get-CimInstance -Class 'Win32_OperatingSystem'
# Check if this operating system needs an update to the ServerManager cmdlets
if ($operatingSystem.Version.StartsWith('6.1.') -and `
$serverCoreOSCodes -contains $operatingSystem.OperatingSystemSKU)
{
Write-Verbose -Message $script:localizedData.EnableServerManagerPSHCmdletsFeature
<#
ServerManager-PSH-Cmdlets has a depndency on Powershell 2 update: MicrosoftWindowsPowerShell,
so enabling the MicrosoftWindowsPowerShell update.
#>
$null = Dism\online\enable-feature\FeatureName:MicrosoftWindowsPowerShell
$null = Dism\online\enable-feature\FeatureName:ServerManager-PSH-Cmdlets
}
try
{
Import-Module -Name 'ServerManager' -ErrorAction Stop
}
catch [System.Management.Automation.RuntimeException]
{
if ($_.Exception.Message -like "*Some or all identity references could not be translated*")
{
Write-Verbose -Message $script:localizedData.IdentityNotFoundMessage
}
else
{
Write-Verbose -Message $script:localizedData.ServerManagerModuleNotFoundMessage
New-InvalidOperationException -Message $script:localizedData.SkuNotSupported
}
}
catch
{
Write-Verbose -Message $script:localizedData.ServerManagerModuleNotFoundMessage
New-InvalidOperationException -Message $script:localizedData.SkuNotSupported
}
}
<#
.SYNOPSIS
Tests if the machine is a Windows Server 2008 R2 SP1 machine.
.NOTES
Since Assert-PrequisitesValid ensures that ServerManager is available on the machine,
this function only checks the OS version.
#>
function Test-IsWinServer2008R2SP1
{
param
()
return ([Environment]::OSVersion.Version.ToString().Contains('6.1.'))
}
Export-ModuleMember -Function *-TargetResource