-
Notifications
You must be signed in to change notification settings - Fork 149
/
MSFT_xWebSite.psm1
2259 lines (1974 loc) · 78.7 KB
/
MSFT_xWebSite.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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#requires -Version 4.0 -Modules CimCmdlets
$script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPath 'Modules'
$script:localizationModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'xWebAdministration.Common'
Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath 'xWebAdministration.Common.psm1')
# Import Localization Strings
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xWebSite'
<#
.SYNOPSYS
The Get-TargetResource cmdlet is used to fetch the status of role or Website on
the target machine. It gives the Website info of the requested role/feature on the
target machine.
.PARAMETER Name
Name of the website
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name
)
Assert-Module
$website = Get-Website | Where-Object -FilterScript {$_.Name -eq $Name}
if ($website.Count -eq 0)
{
Write-Verbose -Message ($script:localizedData.VerboseGetTargetAbsent)
$ensureResult = 'Absent'
}
elseif ($website.Count -eq 1)
{
Write-Verbose -Message ($script:localizedData.VerboseGetTargetPresent)
$ensureResult = 'Present'
$cimBindings = @(ConvertTo-CimBinding -InputObject $website.bindings.Collection)
$allDefaultPages = @(
Get-WebConfiguration -Filter '/system.webServer/defaultDocument/files/*' -PSPath "IIS:\Sites\$Name" |
ForEach-Object -Process {Write-Output -InputObject $_.value}
)
$cimAuthentication = Get-AuthenticationInfo -Site $Name
$websiteAutoStartProviders = (Get-WebConfiguration `
-filter /system.applicationHost/serviceAutoStartProviders).Collection
$webConfiguration = $websiteAutoStartProviders | `
Where-Object -Property Name -eq -Value $ServiceAutoStartProvider | `
Select-Object Name,Type
[Array] $cimLogCustomFields = ConvertTo-CimLogCustomFields -InputObject $website.logFile.customFields.Collection
$logFlagsArray = $null
if ($website.logfile.LogExtFileFlags -is [System.String])
{
$logFlagsArray = [System.String[]] $website.logfile.LogExtFileFlags.Split(',')
}
}
# Multiple websites with the same name exist. This is not supported and is an error
else
{
$errorMessage = $script:localizedData.ErrorWebsiteDiscoveryFailure -f $Name
New-TerminatingError -ErrorId 'WebsiteDiscoveryFailure' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidResult'
}
# Add all website properties to the hash table
return @{
Ensure = $ensureResult
Name = $Name
SiteId = $website.id
PhysicalPath = $website.PhysicalPath
State = $website.State
ApplicationPool = $website.ApplicationPool
BindingInfo = $cimBindings
DefaultPage = $allDefaultPages
EnabledProtocols = $website.EnabledProtocols
ServerAutoStart = $website.serverAutoStart
AuthenticationInfo = $cimAuthentication
PreloadEnabled = $website.applicationDefaults.preloadEnabled
ServiceAutoStartProvider = $website.applicationDefaults.serviceAutoStartProvider
ServiceAutoStartEnabled = $website.applicationDefaults.serviceAutoStartEnabled
ApplicationType = $webConfiguration.Type
LogPath = $website.logfile.directory
LogFlags = $logFlagsArray
LogPeriod = $website.logfile.period
LogtruncateSize = $website.logfile.truncateSize
LoglocalTimeRollover = $website.logfile.localTimeRollover
LogFormat = $website.logfile.logFormat
LogTargetW3C = $website.logfile.logTargetW3C
LogCustomFields = $cimLogCustomFields
}
}
<#
.SYNOPSYS
The Set-TargetResource cmdlet is used to create, delete or configure a website on the
target machine.
.PARAMETER SiteId
Optional. Specifies the IIS site Id for the web site.
.PARAMETER PhysicalPath
Specifies the physical path of the web site. Don't set this if the site will be deployed by an external tool that updates the path.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateSet('Present', 'Absent')]
[String]
$Ensure = 'Present',
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,
# To avoid confusion we use SiteId instead of just Id
[Parameter()]
[UInt32]
$SiteId,
[Parameter()]
[String]
$PhysicalPath,
[Parameter()]
[ValidateSet('Started', 'Stopped')]
[String]
$State = 'Started',
# The application pool name must contain between 1 and 64 characters
[Parameter()]
[ValidateLength(1, 64)]
[String]
$ApplicationPool,
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance[]]
$BindingInfo,
[Parameter()]
[String[]]
$DefaultPage,
[Parameter()]
[String]
$EnabledProtocols,
[Parameter()]
[Boolean]
$ServerAutoStart,
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$AuthenticationInfo,
[Parameter()]
[Boolean]
$PreloadEnabled,
[Parameter()]
[Boolean]
$ServiceAutoStartEnabled,
[Parameter()]
[String]
$ServiceAutoStartProvider,
[Parameter()]
[String]
$ApplicationType,
[Parameter()]
[String]
$LogPath,
[Parameter()]
[ValidateSet('Date','Time','ClientIP','UserName','SiteName','ComputerName','ServerIP','Method','UriStem','UriQuery','HttpStatus','Win32Status','BytesSent','BytesRecv','TimeTaken','ServerPort','UserAgent','Cookie','Referer','ProtocolVersion','Host','HttpSubStatus')]
[String[]]
$LogFlags,
[Parameter()]
[ValidateSet('Hourly','Daily','Weekly','Monthly','MaxSize')]
[String]
$LogPeriod,
[Parameter()]
[ValidateScript({
([ValidateRange(1048576, 4294967295)] $valueAsUInt64 = [UInt64]::Parse($_))
})]
[String]
$LogTruncateSize,
[Parameter()]
[Boolean]
$LoglocalTimeRollover,
[Parameter()]
[ValidateSet('IIS','W3C','NCSA')]
[String]
$LogFormat,
[Parameter()]
[ValidateSet('File','ETW','File,ETW')]
[String]
$LogTargetW3C,
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance[]]
$LogCustomFields
)
Assert-Module
$website = Get-Website | Where-Object -FilterScript {$_.Name -eq $Name}
if ($Ensure -eq 'Present')
{
if ($null -ne $website)
{
# Update Site Id if required
# Note: Set-ItemProperty is case sensitive. only works with id, not Id or ID
if ($SiteId -gt 0 -and `
$website.Id -ne $SiteId)
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name id `
-Value $SiteId `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedSiteId `
-f $Name, $SiteId)
}
# Update Physical Path if required
if ([String]::IsNullOrEmpty($PhysicalPath) -eq $false -and `
$website.PhysicalPath -ne $PhysicalPath)
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name physicalPath `
-Value $PhysicalPath `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedPhysicalPath `
-f $Name, $PhysicalPath)
}
# Update Application Pool if required
if ($PSBoundParameters.ContainsKey('ApplicationPool') -and `
$website.ApplicationPool -ne $ApplicationPool)
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name applicationPool `
-Value $ApplicationPool `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedApplicationPool `
-f $Name, $ApplicationPool)
}
# Update Bindings if required
if ($PSBoundParameters.ContainsKey('BindingInfo') -and `
$null -ne $BindingInfo)
{
if (-not (Test-WebsiteBinding -Name $Name `
-BindingInfo $BindingInfo))
{
Update-WebsiteBinding -Name $Name `
-BindingInfo $BindingInfo
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedBindingInfo `
-f $Name)
}
}
# Update Enabled Protocols if required
if ($PSBoundParameters.ContainsKey('EnabledProtocols') -and `
$website.EnabledProtocols -ne $EnabledProtocols)
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name enabledProtocols `
-Value $EnabledProtocols `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedEnabledProtocols `
-f $Name, $EnabledProtocols)
}
# Update Default Pages if required
if ($PSBoundParameters.ContainsKey('DefaultPage') -and `
$null -ne $DefaultPage)
{
Update-DefaultPage -Name $Name `
-DefaultPage $DefaultPage
}
# Update State if required
if ($PSBoundParameters.ContainsKey('State') -and `
$website.State -ne $State)
{
if ($State -eq 'Started')
{
# Ensure that there are no other running websites with binding information that
# will conflict with this website before starting
if (-not (Confirm-UniqueBinding -Name $Name -ExcludeStopped))
{
# Return error and do not start the website
$errorMessage = $script:localizedData.ErrorWebsiteBindingConflictOnStart `
-f $Name
New-TerminatingError -ErrorId 'WebsiteBindingConflictOnStart' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidResult'
}
try
{
Start-Website -Name $Name -ErrorAction Stop
}
catch
{
$errorMessage = $script:localizedData.ErrorWebsiteStateFailure `
-f $Name, $_.Exception.Message
New-TerminatingError -ErrorId 'WebsiteStateFailure' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidOperation'
}
}
else
{
try
{
Stop-Website -Name $Name -ErrorAction Stop
}
catch
{
$errorMessage = $script:localizedData.ErrorWebsiteStateFailure `
-f $Name, $_.Exception.Message
New-TerminatingError -ErrorId 'WebsiteStateFailure' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidOperation'
}
}
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedState `
-f $Name, $State)
}
# Update ServerAutoStart if required
if ($PSBoundParameters.ContainsKey('ServerAutoStart') -and `
($website.serverAutoStart -ne $ServerAutoStart))
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name serverAutoStart `
-Value $ServerAutoStart `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetWebsiteAutoStartUpdated `
-f $Name)
}
}
# Create website if it does not exist
else
{
try
{
$PSBoundParameters.GetEnumerator() | Where-Object -FilterScript {
$_.Key -in (Get-Command -Name New-Website `
-Module WebAdministration).Parameters.Keys
} | ForEach-Object -Begin {
$newWebsiteSplat = @{}
} -Process {
$newWebsiteSplat.Add($_.Key, $_.Value)
}
# New-WebSite has Id parameter instead of SiteId, so it's getting mapped to Id
if ($PSBoundParameters.ContainsKey('SiteId'))
{
$newWebsiteSplat.Add('Id', $SiteId)
} elseif (-not (Get-WebSite)) {
# If there are no other websites and SiteId is missing, specify the Id Parameter for the new website.
# Otherwise an error can occur on systems running Windows Server 2008 R2.
$newWebsiteSplat.Add('Id', 1)
}
if ([String]::IsNullOrEmpty($PhysicalPath))
{
# If no physical path is provided run New-Website with -Force flag
$website = New-Website @newWebsiteSplat -ErrorAction Stop -Force
} else {
# If physical path is provided don't run New-Website with -Force flag to verify that the path exists
$website = New-Website @newWebsiteSplat -ErrorAction Stop
}
Write-Verbose -Message ($script:localizedData.VerboseSetTargetWebsiteCreated `
-f $Name)
}
catch
{
$errorMessage = $script:localizedData.ErrorWebsiteCreationFailure `
-f $Name, $_.Exception.Message
New-TerminatingError -ErrorId 'WebsiteCreationFailure' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidOperation'
}
Stop-Website -Name $website.Name -ErrorAction Stop
# Clear default bindings if new bindings defined and are different
if ($PSBoundParameters.ContainsKey('BindingInfo') -and `
$null -ne $BindingInfo)
{
if (-not (Test-WebsiteBinding -Name $Name `
-BindingInfo $BindingInfo))
{
Update-WebsiteBinding -Name $Name -BindingInfo $BindingInfo
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedBindingInfo `
-f $Name)
}
}
# Update Enabled Protocols if required
if ($PSBoundParameters.ContainsKey('EnabledProtocols') `
-and $website.EnabledProtocols `
-ne $EnabledProtocols)
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name enabledProtocols `
-Value $EnabledProtocols `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdatedEnabledProtocols `
-f $Name, $EnabledProtocols)
}
# Update Default Pages if required
if ($PSBoundParameters.ContainsKey('DefaultPage') -and `
$null -ne $DefaultPage)
{
Update-DefaultPage -Name $Name `
-DefaultPage $DefaultPage
}
# Start website if required
if ($State -eq 'Started')
{
# Ensure that there are no other running websites with binding information that
# will conflict with this website before starting
if (-not (Confirm-UniqueBinding -Name $Name -ExcludeStopped))
{
# Return error and do not start the website
$errorMessage = $script:localizedData.ErrorWebsiteBindingConflictOnStart `
-f $Name
New-TerminatingError -ErrorId 'WebsiteBindingConflictOnStart' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidResult'
}
try
{
Start-Website -Name $Name -ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetWebsiteStarted `
-f $Name)
}
catch
{
$errorMessage = $script:localizedData.ErrorWebsiteStateFailure `
-f $Name, $_.Exception.Message
New-TerminatingError -ErrorId 'WebsiteStateFailure' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidOperation'
}
}
# Update ServerAutoStart if required
if ($PSBoundParameters.ContainsKey('ServerAutoStart') -and `
($website.serverAutoStart -ne $ServerAutoStart))
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name serverAutoStart `
-Value $ServerAutoStart `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetWebsiteAutoStartUpdated `
-f $Name)
}
}
# Set Authentication; if not defined then pass in DefaultAuthenticationInfo
if ($PSBoundParameters.ContainsKey('AuthenticationInfo') -and `
(-not (Test-AuthenticationInfo -Site $Name `
-AuthenticationInfo $AuthenticationInfo)))
{
Set-AuthenticationInfo -Site $Name `
-AuthenticationInfo $AuthenticationInfo `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetAuthenticationInfoUpdated `
-f $Name)
}
# Update Preload if required
if ($PSBoundParameters.ContainsKey('preloadEnabled') -and `
($website.applicationDefaults.preloadEnabled -ne $PreloadEnabled))
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name applicationDefaults.preloadEnabled `
-Value $PreloadEnabled `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetWebsitePreloadUpdated `
-f $Name)
}
# Update AutoStart if required
if ($PSBoundParameters.ContainsKey('ServiceAutoStartEnabled') -and `
($website.applicationDefaults.ServiceAutoStartEnabled -ne $ServiceAutoStartEnabled))
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name applicationDefaults.serviceAutoStartEnabled `
-Value $ServiceAutoStartEnabled `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetWebsiteAutoStartUpdated `
-f $Name)
}
# Update AutoStartProviders if required
if ($PSBoundParameters.ContainsKey('ServiceAutoStartProvider') -and `
($website.applicationDefaults.ServiceAutoStartProvider `
-ne $ServiceAutoStartProvider))
{
if (-not (Confirm-UniqueServiceAutoStartProviders `
-ServiceAutoStartProvider $ServiceAutoStartProvider `
-ApplicationType $ApplicationType))
{
Add-WebConfiguration -filter /system.applicationHost/serviceAutoStartProviders `
-Value @{
name = $ServiceAutoStartProvider
type = $ApplicationType
} `
-ErrorAction Stop
Write-Verbose -Message `
($script:localizedData.VerboseSetTargetIISAutoStartProviderUpdated)
}
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name applicationDefaults.serviceAutoStartProvider `
-Value $ServiceAutoStartProvider -ErrorAction Stop
Write-Verbose -Message `
($script:localizedData.VerboseSetTargetServiceAutoStartProviderUpdated `
-f $Name)
}
# Update LogFormat if Needed
if ($PSBoundParameters.ContainsKey('LogFormat') -and `
($LogFormat -ne $website.logfile.LogFormat))
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogFormat -f $Name)
# In Windows Server 2008 R2, Set-ItemProperty only accepts index values to the LogFile.LogFormat property
$site = Get-Item "IIS:\Sites\$Name"
$site.LogFile.LogFormat = $LogFormat
$site | Set-Item
}
# Update LogTargetW3C if Needed
if ($PSBoundParameters.ContainsKey('LogTargetW3C') `
-and $website.logfile.LogTargetW3C `
-ne $LogTargetW3C)
{
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name logfile.logTargetW3C `
-Value $LogTargetW3C `
-ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogTargetW3C `
-f $Name, $LogTargetW3C)
}
# Update LogFlags if required
if ($PSBoundParameters.ContainsKey('LogFlags') -and `
(-not (Compare-LogFlags -Name $Name -LogFlags $LogFlags)))
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogFlags `
-f $Name)
Set-ItemProperty "IIS:\Sites\$Name" `
-Name logFile.logFormat -value 'W3C'
Set-ItemProperty "IIS:\Sites\$Name" `
-Name logFile.logExtFileFlags -value ($LogFlags -join ',')
}
# Update LogPath if required
if ($PSBoundParameters.ContainsKey('LogPath') -and `
($LogPath -ne $website.logfile.directory))
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogPath `
-f $Name)
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name LogFile.directory -value $LogPath
}
# Update LogPeriod if needed
if ($PSBoundParameters.ContainsKey('LogPeriod') -and `
($LogPeriod -ne $website.logfile.period))
{
if ($PSBoundParameters.ContainsKey('LogTruncateSize'))
{
Write-Verbose -Message ($script:localizedData.WarningLogPeriod `
-f $Name)
}
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogPeriod)
# In Windows Server 2008 R2, Set-ItemProperty only accepts index values to the LogFile.Period property
$site = Get-Item "IIS:\Sites\$Name"
$site.LogFile.Period = $LogPeriod
$site | Set-Item
}
# Update LogTruncateSize if needed
if ($PSBoundParameters.ContainsKey('LogTruncateSize') -and `
($LogTruncateSize -ne $website.logfile.LogTruncateSize))
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogTruncateSize `
-f $Name)
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name LogFile.truncateSize -Value $LogTruncateSize
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name LogFile.period -Value 'MaxSize'
}
# Update LoglocalTimeRollover if needed
if ($PSBoundParameters.ContainsKey('LoglocalTimeRollover') -and `
($LoglocalTimeRollover -ne `
([System.Convert]::ToBoolean($website.logfile.LocalTimeRollover))))
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLoglocalTimeRollover `
-f $Name)
Set-ItemProperty -Path "IIS:\Sites\$Name" `
-Name LogFile.localTimeRollover -Value $LoglocalTimeRollover
}
# Update LogCustomFields if needed
if ($PSBoundParameters.ContainsKey('LogCustomFields') -and `
(-not (Test-LogCustomField -Site $Name -LogCustomField $LogCustomFields)))
{
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogCustomFields `
-f $Name)
Set-LogCustomField -Site $Name -LogCustomField $LogCustomFields
}
}
# Remove website
else
{
try
{
Remove-Website -Name $Name -ErrorAction Stop
Write-Verbose -Message ($script:localizedData.VerboseSetTargetWebsiteRemoved `
-f $Name)
}
catch
{
$errorMessage = $script:localizedData.ErrorWebsiteRemovalFailure `
-f $Name, $_.Exception.Message
New-TerminatingError -ErrorId 'WebsiteRemovalFailure' `
-ErrorMessage $errorMessage `
-ErrorCategory 'InvalidOperation'
}
}
}
<#
.SYNOPSIS
The Test-TargetResource cmdlet is used to validate if the role or feature is in a state as
expected in the instance document.
.PARAMETER SiteId
Optional. Specifies the IIS site Id for the web site.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([Boolean])]
param
(
[Parameter()]
[ValidateSet('Present', 'Absent')]
[String]
$Ensure = 'Present',
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,
[Parameter()]
[UInt32]
$SiteId,
[Parameter()]
[String]
$PhysicalPath,
[Parameter()]
[ValidateSet('Started', 'Stopped')]
[String]
$State = 'Started',
# The application pool name must contain between 1 and 64 characters
[Parameter()]
[ValidateLength(1, 64)]
[String]
$ApplicationPool,
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance[]]
$BindingInfo,
[Parameter()]
[String[]]
$DefaultPage,
[Parameter()]
[String]
$EnabledProtocols,
[Parameter()]
[Boolean]
$ServerAutoStart,
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$AuthenticationInfo,
[Parameter()]
[Boolean]
$PreloadEnabled,
[Parameter()]
[Boolean]
$ServiceAutoStartEnabled,
[Parameter()]
[String]
$ServiceAutoStartProvider,
[Parameter()]
[String]
$ApplicationType,
[Parameter()]
[String]
$LogPath,
[Parameter()]
[ValidateSet('Date','Time','ClientIP','UserName','SiteName','ComputerName','ServerIP','Method','UriStem','UriQuery','HttpStatus','Win32Status','BytesSent','BytesRecv','TimeTaken','ServerPort','UserAgent','Cookie','Referer','ProtocolVersion','Host','HttpSubStatus')]
[String[]]
$LogFlags,
[Parameter()]
[ValidateSet('Hourly','Daily','Weekly','Monthly','MaxSize')]
[String]
$LogPeriod,
[Parameter()]
[ValidateScript({
([ValidateRange(1048576, 4294967295)] $valueAsUInt64 = [UInt64]::Parse($_))
})]
[String]
$LogTruncateSize,
[Parameter()]
[Boolean]
$LoglocalTimeRollover,
[Parameter()]
[ValidateSet('IIS','W3C','NCSA')]
[String]
$LogFormat,
[Parameter()]
[ValidateSet('File','ETW','File,ETW')]
[String]
$LogTargetW3C,
[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance[]]
$LogCustomFields
)
Assert-Module
$inDesiredState = $true
$website = Get-Website | Where-Object -FilterScript {$_.Name -eq $Name}
# Check Ensure
if (($Ensure -eq 'Present' -and $null -eq $website) -or `
($Ensure -eq 'Absent' -and $null -ne $website))
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseEnsure `
-f $Name)
}
# Only check properties if website exists
if ($Ensure -eq 'Present' -and `
$null -ne $website)
{
# Check Site Id property.
if ($SiteId -gt 0 -and $website.Id -ne $SiteId)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseSiteId -f $Name)
}
# Check Physical Path property
if ($PSBoundParameters.ContainsKey('PhysicalPath') -and `
$website.PhysicalPath -ne $PhysicalPath)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalsePhysicalPath `
-f $Name)
}
# Check State
if ($PSBoundParameters.ContainsKey('State') -and `
$website.State -ne $State)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseState `
-f $Name)
}
# Check Application Pool property
if ($PSBoundParameters.ContainsKey('ApplicationPool') -and `
$website.ApplicationPool -ne $ApplicationPool)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseApplicationPool `
-f $Name)
}
# Check Binding properties
if ($PSBoundParameters.ContainsKey('BindingInfo') -and `
$null -ne $BindingInfo)
{
if (-not (Test-WebsiteBinding -Name $Name -BindingInfo $BindingInfo))
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseBindingInfo `
-f $Name)
}
}
# Check Enabled Protocols
if ($PSBoundParameters.ContainsKey('EnabledProtocols') -and `
$website.EnabledProtocols -ne $EnabledProtocols)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseEnabledProtocols `
-f $Name)
}
# Check Default Pages
if ($PSBoundParameters.ContainsKey('DefaultPage') -and `
$null -ne $DefaultPage)
{
$allDefaultPages = @(
Get-WebConfiguration -Filter '/system.webServer/defaultDocument/files/*' `
-PSPath "IIS:\Sites\$Name" |
ForEach-Object -Process { Write-Output -InputObject $_.value }
)
foreach ($page in $DefaultPage)
{
if ($allDefaultPages -inotcontains $page)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseDefaultPage `
-f $Name)
}
}
}
#Check ServerAutoStart
if ($PSBoundParameters.ContainsKey('ServerAutoStart') -and `
$website.serverAutoStart -ne $ServerAutoStart)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseAutoStart `
-f $Name)
}
#Check AuthenticationInfo
if ($PSBoundParameters.ContainsKey('AuthenticationInfo') -and `
(-not (Test-AuthenticationInfo -Site $Name `
-AuthenticationInfo $AuthenticationInfo)))
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseAuthenticationInfo)
}
#Check Preload
if ($PSBoundParameters.ContainsKey('preloadEnabled') -and `
$website.applicationDefaults.preloadEnabled -ne $PreloadEnabled)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalsePreload `
-f $Name)
}
#Check AutoStartEnabled
if ($PSBoundParameters.ContainsKey('serviceAutoStartEnabled') -and `
$website.applicationDefaults.serviceAutoStartEnabled -ne $ServiceAutoStartEnabled)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseServiceAutoStart `
-f $Name)
}
#Check AutoStartProviders
if ($PSBoundParameters.ContainsKey('serviceAutoStartProvider') -and `
$website.applicationDefaults.serviceAutoStartProvider -ne $ServiceAutoStartProvider)
{
if (-not (Confirm-UniqueServiceAutoStartProviders `
-serviceAutoStartProvider $ServiceAutoStartProvider `
-ApplicationType $ApplicationType))
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseIISAutoStartProvider)
}
}
# Check LogFormat
if ($PSBoundParameters.ContainsKey('LogFormat'))
{
# Warn if LogFlags are passed in and Current LogFormat is not W3C
if ($PSBoundParameters.ContainsKey('LogFlags') -and `
$LogFormat -ne 'W3C')
{
Write-Verbose -Message ($script:localizedData.WarningIncorrectLogFormat `
-f $Name)
}
# Warn if LogFlags are passed in and Desired LogFormat is not W3C
if ($PSBoundParameters.ContainsKey('LogFlags') -and `
$website.logfile.LogFormat -ne 'W3C')
{
Write-Verbose -Message ($script:localizedData.WarningIncorrectLogFormat `
-f $Name)
}
# Check Log Format
if ($LogFormat -ne $website.logfile.LogFormat)
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseLogFormat `
-f $Name)
}
}
# Check LogFlags
if ($PSBoundParameters.ContainsKey('LogFlags') -and `
(-not (Compare-LogFlags -Name $Name -LogFlags $LogFlags)))
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseLogFlags)
}
# Check LogPath
if ($PSBoundParameters.ContainsKey('LogPath') -and `
($LogPath -ne $website.logfile.directory))
{
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseLogPath `
-f $Name)
}
# Check LogPeriod
if ($PSBoundParameters.ContainsKey('LogPeriod') -and `
($LogPeriod -ne $website.logfile.period))
{
if ($PSBoundParameters.ContainsKey('LogTruncateSize'))
{
Write-Verbose -Message ($script:localizedData.WarningLogPeriod `
-f $Name)
}
$inDesiredState = $false
Write-Verbose -Message ($script:localizedData.VerboseTestTargetFalseLogPeriod `
-f $Name)
}
# Check LogTruncateSize
if ($PSBoundParameters.ContainsKey('LogTruncateSize') -and `
($LogTruncateSize -ne $website.logfile.LogTruncateSize))
{