-
Notifications
You must be signed in to change notification settings - Fork 147
/
MSFT_xWebApplication.psm1
925 lines (785 loc) · 31.7 KB
/
MSFT_xWebApplication.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
# Load the Helper Module
Import-Module -Name "$PSScriptRoot\..\Helper.psm1"
# Localized messages
data LocalizedData
{
# culture="en-US"
ConvertFrom-StringData -StringData @'
ErrorWebApplicationTestAutoStartProviderFailure = Desired AutoStartProvider is not valid due to a conflicting Global Property. Ensure that the serviceAutoStartProvider is a unique key.
VerboseGetTargetResource = Get-TargetResource has been run.
VerboseSetTargetAbsent = Removing existing Web Application "{0}".
VerboseSetTargetPresent = Creating new Web application "{0}".
VerboseSetTargetPhysicalPath = Updating physical path for Web application "{0}".
VerboseSetTargetWebAppPool = Updating application pool for Web application "{0}".
VerboseSetTargetSslFlags = Updating SslFlags for Web application "{0}".
VerboseSetTargetAuthenticationInfo = Updating AuthenticationInfo for Web application "{0}".
VerboseSetTargetPreload = Updating Preload for Web application "{0}".
VerboseSetTargetAutostart = Updating AutoStart for Web application "{0}".
VerboseSetTargetIISAutoStartProviders = Updating AutoStartProviders for IIS.
VerboseSetTargetWebApplicationAutoStartProviders = Updating AutoStartProviders for Web application "{0}".
VerboseSetTargetEnabledProtocols = Updating EnabledProtocols for Web application "{0}".
VerboseTestTargetFalseAbsent = Web application "{0}" is absent and should not absent.
VerboseTestTargetFalsePresent = Web application $Name should be absent and is not absent.
VerboseTestTargetFalsePhysicalPath = Physical path for web application "{0}" does not match desired state.
VerboseTestTargetFalseWebAppPool = Web application pool for web application "{0}" does not match desired state.
VerboseTestTargetFalseSslFlags = SslFlags for web application "{0}" are not in the desired state.
VerboseTestTargetFalseAuthenticationInfo = AuthenticationInfo for web application "{0}" is not in the desired state.
VerboseTestTargetFalsePreload = Preload for web application "{0}" is not in the desired state.
VerboseTestTargetFalseAutostart = Autostart for web application "{0}" is not in the desired state.
VerboseTestTargetFalseAutoStartProviders = AutoStartProviders for web application "{0}" are not in the desired state.
VerboseTestTargetFalseIISAutoStartProviders = AutoStartProviders for IIS are not in the desired state.
VerboseTestTargetFalseWebApplicationAutoStartProviders = AutoStartProviders for web application "{0}" are not in the desired state.
VerboseTestTargetFalseEnabledProtocols = EnabledProtocols for web application "{0}" are not in the desired state.
'@
}
<#
.SYNOPSIS
This will return a hashtable of results
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[String] $Website,
[Parameter(Mandatory = $true)]
[String] $Name,
[Parameter(Mandatory = $true)]
[String] $WebAppPool,
[Parameter(Mandatory = $true)]
[String] $PhysicalPath
)
Assert-Module
$webApplication = Get-WebApplication -Site $Website -Name $Name
$cimAuthentication = Get-AuthenticationInfo -Site $Website -Name $Name
$currentSslFlags = (Get-SslFlags -Location "${Website}/${Name}")
$Ensure = 'Absent'
if ($webApplication.Count -eq 1)
{
$Ensure = 'Present'
}
Write-Verbose -Message $LocalizedData.VerboseGetTargetResource
$returnValue = @{
Website = $Website
Name = $Name
WebAppPool = $webApplication.applicationPool
PhysicalPath = $webApplication.PhysicalPath
AuthenticationInfo = $cimAuthentication
SslFlags = [Array]$currentSslFlags
PreloadEnabled = $webApplication.preloadEnabled
ServiceAutoStartProvider = $webApplication.serviceAutoStartProvider
ServiceAutoStartEnabled = $webApplication.serviceAutoStartEnabled
EnabledProtocols = [Array]$webApplication.EnabledProtocols
Ensure = $Ensure
}
return $returnValue
}
<#
.SYNOPSIS
This will set the desired state
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String] $Website,
[Parameter(Mandatory = $true)]
[String] $Name,
[Parameter(Mandatory = $true)]
[String] $WebAppPool,
[Parameter(Mandatory = $true)]
[String] $PhysicalPath,
[Parameter()]
[ValidateSet('Present','Absent')]
[String] $Ensure = 'Present',
[Parameter()]
[AllowEmptyString()]
[ValidateSet('','Ssl','SslNegotiateCert','SslRequireCert','Ssl128')]
[String[]]$SslFlags = '',
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$AuthenticationInfo,
[Parameter()]
[Boolean]
$PreloadEnabled,
[Parameter()]
[Boolean]
$ServiceAutoStartEnabled,
[Parameter()]
[String]
$ServiceAutoStartProvider,
[Parameter()]
[String]
$ApplicationType,
[Parameter()]
[ValidateSet('http','https','net.tcp','net.msmq','net.pipe')]
[String[]] $EnabledProtocols
)
Assert-Module
if ($Ensure -eq 'Present')
{
$webApplication = Get-WebApplication -Site $Website -Name $Name
if ($AuthenticationInfo -eq $null)
{
$AuthenticationInfo = Get-DefaultAuthenticationInfo
}
if ($webApplication.count -eq 0)
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetPresent -f $Name)
New-WebApplication -Site $Website -Name $Name `
-PhysicalPath $PhysicalPath `
-ApplicationPool $WebAppPool
$webApplication = Get-WebApplication -Site $Website -Name $Name
}
# Update Physical Path if required
if (($PSBoundParameters.ContainsKey('PhysicalPath') -and `
$webApplication.physicalPath -ne $PhysicalPath))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetPhysicalPath -f $Name)
#Note: read this before touching the next line of code:
# https://github.com/PowerShell/xWebAdministration/issues/222
Set-WebConfigurationProperty `
-Filter "$($webApplication.ItemXPath)/virtualDirectory[@path='/']" `
-Name physicalPath `
-Value $PhysicalPath
}
# Update AppPool if required
if ($PSBoundParameters.ContainsKey('WebAppPool') -and `
($webApplication.applicationPool -ne $WebAppPool))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetWebAppPool -f $Name)
#Note: read this before touching the next line of code:
# https://github.com/PowerShell/xWebAdministration/issues/222
Set-WebConfigurationProperty `
-Filter $webApplication.ItemXPath `
-Name applicationPool `
-Value $WebAppPool
}
# Update SslFlags if required
if ($PSBoundParameters.ContainsKey('SslFlags') -and `
(-not (Test-SslFlags -Location "${Website}/${Name}" -SslFlags $SslFlags)))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetSslFlags -f $Name)
$params = @{
PSPath = 'MACHINE/WEBROOT/APPHOST'
Location = "${Website}/${Name}"
Filter = 'system.webServer/security/access'
Name = 'sslFlags'
Value = ($sslflags -join ',')
}
Set-WebConfigurationProperty @params
}
# Set Authentication; if not defined then pass in DefaultAuthenticationInfo
if ($PSBoundParameters.ContainsKey('AuthenticationInfo') -and `
(-not (Test-AuthenticationInfo -Site $Website `
-Name $Name `
-AuthenticationInfo $AuthenticationInfo)))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetAuthenticationInfo -f $Name)
Set-AuthenticationInfo -Site $Website `
-Name $Name `
-AuthenticationInfo $AuthenticationInfo `
-ErrorAction Stop `
-Verbose
}
# Update Preload if required
if ($PSBoundParameters.ContainsKey('preloadEnabled') -and `
$webApplication.preloadEnabled -ne $PreloadEnabled)
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetPreload -f $Name)
Set-ItemProperty -Path "IIS:\Sites\$Website\$Name" `
-Name preloadEnabled `
-Value $preloadEnabled `
-ErrorAction Stop
}
# Update AutoStart if required
if ($PSBoundParameters.ContainsKey('ServiceAutoStartEnabled') -and `
$webApplication.serviceAutoStartEnabled -ne $ServiceAutoStartEnabled)
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetAutostart -f $Name)
Set-ItemProperty -Path "IIS:\Sites\$Website\$Name" `
-Name serviceAutoStartEnabled `
-Value $serviceAutoStartEnabled `
-ErrorAction Stop
}
# Update AutoStartProviders if required
if ($PSBoundParameters.ContainsKey('ServiceAutoStartProvider') -and `
$webApplication.serviceAutoStartProvider -ne $ServiceAutoStartProvider)
{
if (-not (Confirm-UniqueServiceAutoStartProviders `
-ServiceAutoStartProvider $ServiceAutoStartProvider `
-ApplicationType $ApplicationType))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetIISAutoStartProviders)
Add-WebConfiguration `
-filter /system.applicationHost/serviceAutoStartProviders `
-Value @{name=$ServiceAutoStartProvider; type=$ApplicationType} `
-ErrorAction Stop
}
Write-Verbose -Message `
($LocalizedData.VerboseSetTargetWebApplicationAutoStartProviders -f $Name)
Set-ItemProperty -Path "IIS:\Sites\$Website\$Name" `
-Name serviceAutoStartProvider `
-Value $ServiceAutoStartProvider `
-ErrorAction Stop
}
# Update EnabledProtocols if required
if ($PSBoundParameters.ContainsKey('EnabledProtocols') -and `
(-not(Confirm-UniqueEnabledProtocols `
-ExistingProtocols $webApplication.EnabledProtocols `
-ProposedProtocols $EnabledProtocols )))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetEnabledProtocols -f $Name)
# Make input bindings which are an array, into a string
$stringafiedEnabledProtocols = $EnabledProtocols -join ','
Set-ItemProperty -Path "IIS:\Sites\$Website\$Name" `
-Name 'enabledProtocols' `
-Value $stringafiedEnabledProtocols `
-ErrorAction Stop
}
}
if ($Ensure -eq 'Absent')
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetAbsent -f $Name)
Remove-WebApplication -Site $Website -Name $Name
}
}
<#
.SYNOPSIS
This tests the desired state. If the state is not correct it will return $false.
If the state is correct it will return $true
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String] $Website,
[Parameter(Mandatory = $true)]
[String] $Name,
[Parameter(Mandatory = $true)]
[String] $WebAppPool,
[Parameter(Mandatory = $true)]
[String] $PhysicalPath,
[Parameter()]
[ValidateSet('Present','Absent')]
[String] $Ensure = 'Present',
[Parameter()]
[AllowEmptyString()]
[ValidateSet('','Ssl','SslNegotiateCert','SslRequireCert','Ssl128')]
[String[]]$SslFlags = '',
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$AuthenticationInfo,
[Parameter()]
[Boolean]
$preloadEnabled,
[Parameter()]
[Boolean]
$serviceAutoStartEnabled,
[Parameter()]
[String]
$serviceAutoStartProvider,
[Parameter()]
[String]
$ApplicationType,
[Parameter()]
[ValidateSet('http','https','net.tcp','net.msmq','net.pipe')]
[String[]] $EnabledProtocols
)
Assert-Module
$webApplication = Get-WebApplication -Site $Website -Name $Name
if ($AuthenticationInfo -eq $null)
{
$AuthenticationInfo = Get-DefaultAuthenticationInfo
}
if ($webApplication.count -eq 0 -and $Ensure -eq 'Present')
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseAbsent -f $Name)
return $false
}
if ($webApplication.count -eq 1 -and $Ensure -eq 'Absent')
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalsePresent -f $Name)
return $false
}
if ($webApplication.count -eq 1 -and $Ensure -eq 'Present')
{
#Check Physical Path
if ($webApplication.physicalPath -ne $PhysicalPath)
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalsePhysicalPath -f $Name)
return $false
}
#Check AppPool
if ($webApplication.applicationPool -ne $WebAppPool)
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseWebAppPool -f $Name)
return $false
}
#Check SslFlags
if ($PSBoundParameters.ContainsKey('SslFlags') -and `
(-not (Test-SslFlags -Location "${Website}/${Name}" -SslFlags $SslFlags)))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseSslFlags -f $Name)
return $false
}
#Check AuthenticationInfo
if ($PSBoundParameters.ContainsKey('AuthenticationInfo') -and `
(-not (Test-AuthenticationInfo -Site $Website `
-Name $Name `
-AuthenticationInfo $AuthenticationInfo)))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseAuthenticationInfo `
-f $Name)
return $false
}
#Check Preload
if ($PSBoundParameters.ContainsKey('preloadEnabled') -and `
$webApplication.preloadEnabled -ne $PreloadEnabled)
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalsePreload -f $Name)
return $false
}
#Check AutoStartEnabled
if($PSBoundParameters.ContainsKey('ServiceAutoStartEnabled') -and `
$webApplication.serviceAutoStartEnabled -ne $ServiceAutoStartEnabled)
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseAutostart -f $Name)
return $false
}
#Check AutoStartProviders
if ($PSBoundParameters.ContainsKey('ServiceAutoStartProvider') -and `
$webApplication.serviceAutoStartProvider -ne $ServiceAutoStartProvider)
{
if (-not (Confirm-UniqueServiceAutoStartProviders `
-serviceAutoStartProvider $ServiceAutoStartProvider `
-ApplicationType $ApplicationType))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseIISAutoStartProviders)
return $false
}
Write-Verbose -Message `
($LocalizedData.VerboseTestTargetFalseWebApplicationAutoStartProviders -f $Name)
return $false
}
# Update EnabledProtocols if required
if ($PSBoundParameters.ContainsKey('EnabledProtocols') -and `
(-not(Confirm-UniqueEnabledProtocols `
-ExistingProtocols $webApplication.EnabledProtocols `
-ProposedProtocols $EnabledProtocols )))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseEnabledProtocols `
-f $Name)
return $false
}
}
return $true
}
<#
.SYNOPSIS
Helper function used to validate that the EnabledProtocols are unique.
Returns $false if EnabledProtocols are not unique and $true if they are
.PARAMETER ExistingProtocols
Specifies existing SMTP bindings
.PARAMETER ProposedProtocols
Specifies desired SMTP bindings.
.NOTES
ExistingProtocols is a String whereas ProposedProtocols is an array of Strings
so we need to do some extra work in comparing them
#>
function Confirm-UniqueEnabledProtocols
{
[CmdletBinding()]
[OutputType([Boolean])]
param
(
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[String] $ExistingProtocols,
[Parameter(Mandatory = $true)]
[String[]] $ProposedProtocols
)
$inputToCheck = @()
foreach ($proposedProtocol in $ProposedProtocols)
{
$inputToCheck += $proposedProtocol
}
$existingProtocolsToCheck = $existingProtocols -split ','
$existingToCheck = @()
foreach ($existingProtocol in $existingProtocolsToCheck)
{
$existingToCheck += $existingProtocol.Trim()
}
$sortedExistingProtocols = $existingToCheck | Sort-Object -Unique
$sortedInputProtocols = $inputToCheck | Sort-Object -Unique
if (Compare-Object -ReferenceObject $sortedExistingProtocols `
-DifferenceObject $sortedInputProtocols `
-PassThru)
{
return $false
}
return $true
}
function Export-TargetResource
{
[CmdletBinding()]
[OutputType([System.String])]
$InformationPreference = "Continue"
Write-Information "Extracting xWebApplication..."
$webSites = Get-WebSite
$i = 1
foreach($website in $webSites)
{
Write-Information " [$i/$($webSites.Count)] Site {$($webSite.Name)}"
$webApplications = Get-WebApplication -Site $website.name
if($webApplications)
{
$j = 1
foreach($webapplication in $webApplications)
{
Write-Information "[$j/$($webApplications.Count)] $($webApplication.Path)"
<# Setting Primary Keys #>
$params =@{
Name = $webapplication.Path
Website = $website.Name
WebAppPool = $webapplication.ApplicationPool
PhysicalPath = "*"
}
<# Setting Required Keys #>
#$params.WebAppPool = $webapplication.applicationpool
#$params.PhysicalPath = $webapplication.PhysicalPath
Write-Verbose "Key parameters as follows"
$params | ConvertTo-Json | Write-Verbose
$results = Get-TargetResource @params
Write-Verbose "All Parameters as follows"
$results | ConvertTo-Json | Write-Verbose
$AuthenticationInfo = "MSFT_xWebApplicationAuthenticationInformation`r`n {`r`n"
$AuthenticationTypes = @("BasicAuthentication","AnonymousAuthentication","DigestAuthentication","WindowsAuthentication")
foreach ($authenticationtype in $AuthenticationTypes)
{
Remove-Variable -Name location -ErrorAction SilentlyContinue
Remove-Variable -Name prop -ErrorAction SilentlyContinue
$location = "$($website.Name)" + "$($webapplication.Path)"
$prop = Get-WebConfigurationProperty `
-Filter /system.WebServer/security/authentication/$authenticationtype `
-Name enabled `
-PSPath "IIS:\Sites\$location"
Write-Verbose "$authenticationtype : $($prop.Value)"
$AuthenticationInfo += " $($authenticationtype.Replace('Authentication','')) = `$" + $prop.Value + ";`r`n"
}
$results.AuthenticationInfo = $AuthenticationInfo
$results.SslFlags = $results.SslFlags.Split(",")
$results.EnabledProtocols = $results.EnabledProtocols.Split(",")
Write-Verbose "All Parameters with values"
$results | ConvertTo-Json | Write-Verbose
$Script:dscConfigContent += " xWebApplication " + (New-GUID).ToString() + "`r`n {`r`n"
$Script:dscConfigContent += Get-DSCBlock -Params $results -ModulePath $PSScriptRoot
$Script:dscConfigContent += " }`r`n"
$j++
}
}
$i++
}
}
#region Helper Functions
<#
.SYNOPSIS
Helper function used to validate that the AutoStartProviders is unique to other
websites. Returns False if the AutoStartProviders exist.
.PARAMETER serviceAutoStartProvider
Specifies the name of the AutoStartProviders.
.PARAMETER ExcludeStopped
Specifies the name of the Application Type for the AutoStartProvider.
.NOTES
This tests for the existance of a AutoStartProviders which is globally assigned.
As AutoStartProviders need to be uniquely named it will check for this and error out if
attempting to add a duplicatly named AutoStartProvider.
Name is passed in to bubble to any error messages during the test.
#>
function Confirm-UniqueServiceAutoStartProviders
{
[CmdletBinding()]
[OutputType([Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String] $ServiceAutoStartProvider,
[Parameter(Mandatory = $true)]
[String] $ApplicationType
)
$WebSiteAutoStartProviders = (Get-WebConfiguration `
-filter /system.applicationHost/serviceAutoStartProviders).Collection
$ExistingObject = $WebSiteAutoStartProviders | `
Where-Object -Property Name -eq -Value $serviceAutoStartProvider | `
Select-Object Name,Type
$ProposedObject = @(New-Object -TypeName PSObject -Property @{
name = $ServiceAutoStartProvider
type = $ApplicationType
})
if(-not $ExistingObject)
{
return $false
}
if(-not (Compare-Object -ReferenceObject $ExistingObject `
-DifferenceObject $ProposedObject `
-Property name))
{
if(Compare-Object -ReferenceObject $ExistingObject `
-DifferenceObject $ProposedObject `
-Property type)
{
$ErrorMessage = $LocalizedData.ErrorWebApplicationTestAutoStartProviderFailure
New-TerminatingError `
-ErrorId 'ErrorWebApplicationTestAutoStartProviderFailure' `
-ErrorMessage $ErrorMessage `
-ErrorCategory 'InvalidResult'
}
}
return $true
}
<#
.SYNOPSIS
Helper function used to validate that the authenticationProperties for an Application.
.PARAMETER Site
Specifies the name of the Website.
.PARAMETER Name
Specifies the name of the Application.
#>
function Get-AuthenticationInfo
{
[CmdletBinding()]
[OutputType([Microsoft.Management.Infrastructure.CimInstance])]
param
(
[Parameter(Mandatory = $true)]
[String] $Site,
[Parameter(Mandatory = $true)]
[String] $Name
)
$authenticationProperties = @{}
foreach ($type in @('Anonymous', 'Basic', 'Digest', 'Windows'))
{
$authenticationProperties[$type] = [Boolean](Test-AuthenticationEnabled -Site $Site `
-Name $Name `
-Type $type)
}
return New-CimInstance `
-ClassName MSFT_xWebApplicationAuthenticationInformation `
-ClientOnly -Property $authenticationProperties `
-NameSpace 'root\microsoft\windows\desiredstateconfiguration'
}
<#
.SYNOPSIS
Helper function used to build a default CimInstance for AuthenticationInformation
#>
function Get-DefaultAuthenticationInfo
{
New-CimInstance -ClassName MSFT_xWebApplicationAuthenticationInformation `
-ClientOnly `
-Property @{Anonymous=$false;Basic=$false;Digest=$false;Windows=$false} `
-NameSpace 'root\microsoft\windows\desiredstateconfiguration'
}
<#
.SYNOPSIS
Helper function used to return the SSLFlags on an Application.
.PARAMETER Location
Specifies the path in the IIS: PSDrive to the Application
#>
function Get-SslFlags
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)]
[String] $Location
)
$SslFlags = Get-WebConfiguration `
-PSPath IIS:\Sites `
-Location $Location `
-Filter 'system.webserver/security/access' | `
ForEach-Object { $_.sslFlags }
if ($null -eq $SslFlags)
{
return [String]::Empty
}
return $SslFlags
}
<#
.SYNOPSIS
Helper function used to set authenticationProperties for an Application.
.PARAMETER Site
Specifies the name of the Website.
.PARAMETER Name
Specifies the name of the Application.
.PARAMETER Type
Specifies the type of Authentication,
Limited to the set: ('Anonymous','Basic','Digest','Windows').
.PARAMETER Enabled
Whether the Authentication is enabled or not.
#>
function Set-Authentication
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String] $Site,
[Parameter(Mandatory = $true)]
[String] $Name,
[Parameter(Mandatory = $true)]
[ValidateSet('Anonymous','Basic','Digest','Windows')]
[String] $Type,
[Parameter()]
[Boolean] $Enabled
)
Set-WebConfigurationProperty `
-Filter /system.WebServer/security/authentication/${Type}Authentication `
-Name enabled `
-Value $Enabled `
-Location "${Site}/${Name}"
}
<#
.SYNOPSIS
Helper function used to validate that the authenticationProperties for an Application.
.PARAMETER Site
Specifies the name of the Website.
.PARAMETER Name
Specifies the name of the Application.
.PARAMETER AuthenticationInfo
A CimInstance of what state the AuthenticationInfo should be.
#>
function Set-AuthenticationInfo
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String] $Site,
[Parameter(Mandatory = $true)]
[String] $Name,
[Parameter()]
[ValidateNotNullOrEmpty()]
[Microsoft.Management.Infrastructure.CimInstance] $AuthenticationInfo
)
foreach ($type in @('Anonymous', 'Basic', 'Digest', 'Windows'))
{
$enabled = ($AuthenticationInfo.CimInstanceProperties[$type].Value -eq $true)
Set-Authentication -Site $Site `
-Name $Name `
-Type $type `
-Enabled $enabled
}
}
<#
.SYNOPSIS
Helper function used to test the authenticationProperties state for an Application.
Will return that value which will either [String]True or [String]False
.PARAMETER Site
Specifies the name of the Website.
.PARAMETER Name
Specifies the name of the Application.
.PARAMETER Type
Specifies the type of Authentication,
limited to the set: ('Anonymous','Basic','Digest','Windows').
#>
function Test-AuthenticationEnabled
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String] $Site,
[Parameter(Mandatory = $true)]
[String] $Name,
[Parameter(Mandatory = $true)]
[ValidateSet('Anonymous','Basic','Digest','Windows')]
[String] $Type
)
$prop = Get-WebConfigurationProperty `
-Filter /system.WebServer/security/authentication/${Type}Authentication `
-Name enabled `
-Location "${Site}/${Name}"
return $prop.Value
}
<#
.SYNOPSIS
Helper function used to test the authenticationProperties state for an Application.
Will return that result which will either [boolean]$True or [boolean]$False for use in
Test-TargetResource.
Uses Test-AuthenticationEnabled to determine this. First incorrect result will break
this function out.
.PARAMETER Site
Specifies the name of the Website.
.PARAMETER Name
Specifies the name of the Application.
.PARAMETER AuthenticationInfo
A CimInstance of what state the AuthenticationInfo should be.
#>
function Test-AuthenticationInfo
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String] $Site,
[Parameter(Mandatory = $true)]
[String] $Name,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Microsoft.Management.Infrastructure.CimInstance] $AuthenticationInfo
)
foreach ($type in @('Anonymous', 'Basic', 'Digest', 'Windows'))
{
$expected = $AuthenticationInfo.CimInstanceProperties[$type].Value
$actual = Test-AuthenticationEnabled -Site $Site `
-Name $Name `
-Type $type
if ($expected -ne $actual)
{
return $false
}
}
return $true
}
<#
.SYNOPSIS
Helper function used to test the SSLFlags on an Application.
Will return $true if they match and $false if they do not.
.PARAMETER SslFlags
Specifies the SslFlags to Test
.PARAMETER Location
Specifies the path in the IIS: PSDrive to the Application
#>
function Test-SslFlags
{
[CmdletBinding()]
[OutputType([Boolean])]
param
(
[Parameter()]
[AllowEmptyString()]
[ValidateSet('','Ssl','SslNegotiateCert','SslRequireCert','Ssl128')]
[String[]] $SslFlags = '',
[Parameter(Mandatory = $true)]
[String] $Location
)
$CurrentSslFlags = Get-SslFlags -Location $Location
if(Compare-Object -ReferenceObject $CurrentSslFlags `
-DifferenceObject $SslFlags)
{
return $false
}
return $true
}
#endregion
Export-ModuleMember -Function *-TargetResource