-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathMSFT_xIisLogging.psm1
More file actions
590 lines (487 loc) · 20.3 KB
/
Copy pathMSFT_xIisLogging.psm1
File metadata and controls
590 lines (487 loc) · 20.3 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
# Load the Helper Module
Import-Module -Name "$PSScriptRoot\..\Helper.psm1"
# Localized messages
data LocalizedData
{
# culture="en-US"
ConvertFrom-StringData -StringData @'
VerboseGetTargetResult = Get-TargetResource has been run.
VerboseSetTargetUpdateLogPath = LogPath is not in the desired state and will be updated.
VerboseSetTargetUpdateLogFlags = LogFlags do not match and will be updated.
VerboseSetTargetUpdateLogPeriod = LogPeriod is not in the desired state and will be updated.
VerboseSetTargetUpdateLogTruncateSize = TruncateSize is not in the desired state and will be updated.
VerboseSetTargetUpdateLoglocalTimeRollover = LoglocalTimeRollover is not in the desired state and will be updated.
VerboseSetTargetUpdateLogFormat = LogFormat is not in the desired state and will be updated
VerboseSetTargetUpdateLogTargetW3C = LogTargetW3C is not in the desired state and will be updated
VerboseSetTargetUpdateLogCustomFields = LogCustomFields is not in the desired state and will be updated.
VerboseTestTargetUpdateLogCustomFields = LogCustomFields is not in the desired state and will be updated.
VerboseTestTargetFalseLogPath = LogPath does match desired state.
VerboseTestTargetFalseLogFlags = LogFlags does not match desired state.
VerboseTestTargetFalseLogPeriod = LogPeriod does not match desired state.
VerboseTestTargetFalseLogTruncateSize = LogTruncateSize does not match desired state.
VerboseTestTargetFalseLoglocalTimeRollover = LoglocalTimeRollover does not match desired state.
VerboseTestTargetFalseLogFormat = LogFormat does not match desired state.
VerboseTestTargetFalseLogTargetW3C = LogTargetW3C does not match desired state.
WarningLogPeriod = LogTruncateSize has is an input as will overwrite this desired state.
WarningIncorrectLogFormat = LogFormat is not W3C, as a result LogFlags will not be used.
'@
}
<#
.SYNOPSIS
This will return a hashtable of results about the given LogPath
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[String] $LogPath
)
Assert-Module
$currentLogSettings = Get-WebConfiguration `
-filter '/system.applicationHost/sites/siteDefaults/Logfile'
Write-Verbose -Message ($LocalizedData.VerboseGetTargetResult)
$cimLogCustomFields = @(ConvertTo-CimLogCustomFields -InputObject $currentLogSettings.logFile.customFields.Collection)
return @{
LogPath = $currentLogSettings.directory
LogFlags = [Array]$currentLogSettings.LogExtFileFlags
LogPeriod = $currentLogSettings.period
LogTruncateSize = $currentLogSettings.truncateSize
LoglocalTimeRollover = $currentLogSettings.localTimeRollover
LogFormat = $currentLogSettings.logFormat
LogTargetW3C = $currentLogSettings.logTargetW3C
LogCustomFields = $cimLogCustomFields
}
}
<#
.SYNOPSIS
This will set the desired state
.PARAMETER LogPath
Path to the logfile
.PARAMETER LogFlags
Specifies flags to check
Limited to the set: ('Date','Time','ClientIP','UserName','SiteName','ComputerName','ServerIP','Method','UriStem','UriQuery','HttpStatus','Win32Status','BytesSent','BytesRecv','TimeTaken','ServerPort','UserAgent','Cookie','Referer','ProtocolVersion','Host','HttpSubStatus')
.PARAMETER LogPeriod
Specifies the log period.
Limited to the set: ('Hourly','Daily','Weekly','Monthly','MaxSize')
.PARAMETER LogTruncateSize
Specifies log truncate size
Limited to the range (1048576 - 4294967295)
.PARAMETER LoglocalTimeRollover
Sets log local time rollover
.PARAMETER LogFormat
Specifies log format
Limited to the set: ('IIS','W3C','NCSA')
.PARAMETER LogTargetW3C
Specifies W3C log format
Limited to the set: ('File','ETW','File,ETW')
.PARAMETER LogCustomField
A CimInstance collection of what state the LogCustomField should be.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[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
$currentLogState = Get-TargetResource -LogPath $LogPath
# Update LogFormat if needed
if ($PSBoundParameters.ContainsKey('LogFormat') -and `
($LogFormat -ne $currentLogState.LogFormat))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLogFormat)
Set-WebConfigurationProperty '/system.applicationHost/sites/siteDefaults/logfile' `
-Name logFormat `
-Value $LogFormat
}
# Update LogPath if needed
if ($PSBoundParameters.ContainsKey('LogPath') -and ($LogPath -ne $currentLogState.LogPath))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLogPath)
Set-WebConfigurationProperty '/system.applicationHost/sites/siteDefaults/logfile' `
-Name directory `
-Value $LogPath
}
# Update Logflags if needed; also sets logformat to W3C
if ($PSBoundParameters.ContainsKey('LogFlags') -and `
(-not (Compare-LogFlags -LogFlags $LogFlags)))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLogFlags)
Set-WebConfigurationProperty '/system.Applicationhost/Sites/SiteDefaults/logfile' `
-Name logFormat `
-Value 'W3C'
Set-WebConfigurationProperty '/system.Applicationhost/Sites/SiteDefaults/logfile' `
-Name logExtFileFlags `
-Value ($LogFlags -join ',')
}
# Update Log Period if needed
if ($PSBoundParameters.ContainsKey('LogPeriod') -and `
($LogPeriod -ne $currentLogState.LogPeriod))
{
if ($PSBoundParameters.ContainsKey('LogTruncateSize'))
{
Write-Verbose -Message ($LocalizedData.WarningLogPeriod)
}
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLogPeriod)
Set-WebConfigurationProperty '/system.Applicationhost/Sites/SiteDefaults/logfile' `
-Name period `
-Value $LogPeriod
}
# Update LogTruncateSize if needed
if ($PSBoundParameters.ContainsKey('LogTruncateSize') -and `
($LogTruncateSize -ne $currentLogState.LogTruncateSize))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLogTruncateSize)
Set-WebConfigurationProperty '/system.Applicationhost/Sites/SiteDefaults/logfile' `
-Name truncateSize `
-Value $LogTruncateSize
Set-WebConfigurationProperty '/system.Applicationhost/Sites/SiteDefaults/logfile' `
-Name period `
-Value 'MaxSize'
}
# Update LoglocalTimeRollover if needed
if ($PSBoundParameters.ContainsKey('LoglocalTimeRollover') -and `
($LoglocalTimeRollover -ne `
([System.Convert]::ToBoolean($currentLogState.LoglocalTimeRollover))))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLoglocalTimeRollover)
Set-WebConfigurationProperty '/system.Applicationhost/Sites/SiteDefaults/logfile' `
-Name localTimeRollover `
-Value $LoglocalTimeRollover
}
# Update LogTargetW3C if needed
if ($PSBoundParameters.ContainsKey('LogTargetW3C') -and `
($LogTargetW3C -ne $currentLogState.LogTargetW3C))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLogTargetW3C)
Set-WebConfigurationProperty '/system.applicationHost/sites/siteDefaults/logfile' `
-Name logTargetW3C `
-Value $LogTargetW3C
}
# Update LogCustomFields if neeed
if ($PSBoundParameters.ContainsKey('LogCustomFields') -and `
(-not (Test-LogCustomField -LogCustomField $LogCustomFields)))
{
Write-Verbose -Message ($LocalizedData.VerboseSetTargetUpdateLogCustomFields)
Set-LogCustomField -LogCustomField $LogCustomFields
}
}
<#
.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
.PARAMETER LogPath
Path to the logfile
.PARAMETER LogFlags
Specifies flags to check
Limited to the set: ('Date','Time','ClientIP','UserName','SiteName','ComputerName','ServerIP','Method','UriStem','UriQuery','HttpStatus','Win32Status','BytesSent','BytesRecv','TimeTaken','ServerPort','UserAgent','Cookie','Referer','ProtocolVersion','Host','HttpSubStatus')
.PARAMETER LogPeriod
Specifies the log period.
Limited to the set: ('Hourly','Daily','Weekly','Monthly','MaxSize')
.PARAMETER LogTruncateSize
Specifies log truncate size
Limited to the range (1048576 - 4294967295)
.PARAMETER LoglocalTimeRollover
Sets log local time rollover
.PARAMETER LogFormat
Specifies log format
Limited to the set: ('IIS','W3C','NCSA')
.PARAMETER LogTargetW3C
Specifies W3C log format
Limited to the set: ('File','ETW','File,ETW')
.PARAMETER LogCustomField
A CimInstance collection of what state the LogCustomField should be.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[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
$currentLogState = Get-TargetResource -LogPath $LogPath
# 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 ($LocalizedData.WarningIncorrectLogFormat)
}
# Warn if LogFlags are passed in and Desired LogFormat is not W3C
if($PSBoundParameters.ContainsKey('LogFlags') -and `
$currentLogState.LogFormat -ne 'W3C')
{
Write-Verbose -Message ($LocalizedData.WarningIncorrectLogFormat)
}
# Check LogFormat
if ($LogFormat -ne $currentLogState.LogFormat)
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseLogFormat)
return $false
}
}
# Check LogFlags
if ($PSBoundParameters.ContainsKey('LogFlags') -and `
(-not (Compare-LogFlags -LogFlags $LogFlags)))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseLogFlags)
return $false
}
# Check LogPath
if ($PSBoundParameters.ContainsKey('LogPath') -and `
($LogPath -ne $currentLogState.LogPath))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseLogPath)
return $false
}
# Check LogPeriod
if ($PSBoundParameters.ContainsKey('LogPeriod') -and `
($LogPeriod -ne $currentLogState.LogPeriod))
{
if ($PSBoundParameters.ContainsKey('LogTruncateSize'))
{
Write-Verbose -Message ($LocalizedData.WarningLogPeriod)
}
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseLogPeriod)
return $false
}
# Check LogTruncateSize
if ($PSBoundParameters.ContainsKey('LogTruncateSize') -and `
($LogTruncateSize -ne $currentLogState.LogTruncateSize))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseLogTruncateSize)
return $false
}
# Check LoglocalTimeRollover
if ($PSBoundParameters.ContainsKey('LoglocalTimeRollover') -and `
($LoglocalTimeRollover -ne `
([System.Convert]::ToBoolean($currentLogState.LoglocalTimeRollover))))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseLoglocalTimeRollover)
return $false
}
# Check LogTargetW3C
if ($PSBoundParameters.ContainsKey('LogTargetW3C') -and `
($LogTargetW3C -ne $currentLogState.LogTargetW3C))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetFalseLogTargetW3C)
return $false
}
# Check LogCustomFields if neeed
if ($PSBoundParameters.ContainsKey('LogCustomFields') -and `
(-not (Test-LogCustomField -LogCustomFields $LogCustomFields)))
{
Write-Verbose -Message ($LocalizedData.VerboseTestTargetUpdateLogCustomFields)
return $false
}
return $true
}
function Export-TargetResource
{
[CmdletBinding()]
[OutputType([System.String])]
param()
$InformationPreference = 'Continue'
Write-Information 'Extracting xIISLogging...'
$LogSettings = Get-WebConfiguration -Filter '/system.applicationHost/sites/siteDefaults/Logfile'
$params = @{
LogPath = $LogSettings.directory
}
$sb = [System.Text.StringBuilder]::new()
$results = Get-TargetResource @params
$results.LogFlags = $results.LogFlags.Split(',')
[void]$sb.AppendLine(' xIISLogging ' + (New-Guid).ToString())
[void]$sb.AppendLine(' {')
$dscBlock = Get-DSCBlock -Params $results -ModulePath $PSScriptRoot
[void]$sb.Append($dscBlock)
[void]$sb.AppendLine(' }')
return $sb.ToString()
}
#region Helper functions
<#
.SYNOPSIS
Helper function used to validate the logflags status.
Returns False if the loglfags do not match and true if they do
.PARAMETER LogFlags
Specifies flags to check
#>
function Compare-LogFlags
{
[CmdletBinding()]
[OutputType([Boolean])]
param
(
[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
)
$currentLogFlags = (Get-WebConfigurationProperty `
-Filter '/system.Applicationhost/Sites/SiteDefaults/logfile' `
-Name LogExtFileFlags) -split ',' | `
Sort-Object
$proposedLogFlags = $LogFlags -split ',' | Sort-Object
if (Compare-Object -ReferenceObject $currentLogFlags `
-DifferenceObject $proposedLogFlags)
{
return $false
}
return $true
}
<#
.SYNOPSIS
Converts IIS custom log field collection to instances of the MSFT_xLogCustomField CIM class.
.PARAMETER InputObject
Specifies input object passed in
#>
function ConvertTo-CimLogCustomFields
{
[CmdletBinding()]
[OutputType([Microsoft.Management.Infrastructure.CimInstance])]
param
(
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[AllowNull()]
[Object[]] $InputObject
)
$cimClassName = 'MSFT_xLogCustomField'
$cimNamespace = 'root/microsoft/Windows/DesiredStateConfiguration'
$cimCollection = New-Object -TypeName 'System.Collections.ObjectModel.Collection`1[Microsoft.Management.Infrastructure.CimInstance]'
foreach ($customField in $InputObject)
{
$cimProperties = @{
LogFieldName = $customField.LogFieldName
SourceName = $customField.SourceName
SourceType = $customField.SourceType
}
$cimCollection += (New-CimInstance -ClassName $cimClassName `
-Namespace $cimNamespace `
-Property $cimProperties `
-ClientOnly)
}
return $cimCollection
}
<#
.SYNOPSIS
Helper function used to set the LogCustomField for a website.
.PARAMETER LogCustomField
A CimInstance collection of what the LogCustomField should be.
#>
function Set-LogCustomField
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Microsoft.Management.Infrastructure.CimInstance[]]
$LogCustomField
)
$setCustomFields = @()
foreach ($customField in $LogCustomField)
{
$setCustomFields += @{
logFieldName = $customField.LogFieldName
sourceName = $customField.SourceName
sourceType = $customField.SourceType
}
}
<#
Set-WebConfigurationProperty updates logfile.customFields.
#>
Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/Sites/SiteDefaults/logFile/customFields" -Name "." -Value $setCustomFields
}
<#
.SYNOPSIS
Helper function used to test the LogCustomField state for a website.
.PARAMETER LogCustomField
A CimInstance collection of what state the LogCustomField should be.
#>
function Test-LogCustomField
{
[CmdletBinding()]
[OutputType([Boolean])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Microsoft.Management.Infrastructure.CimInstance[]]
$LogCustomFields
)
$inDesiredSate = $true
foreach ($customField in $LogCustomFields)
{
$filterString = "/system.Applicationhost/Sites/SiteDefaults/logFile/customFields/add[@logFieldName='{0}']" -f $customField.LogFieldName
$presentCustomField = Get-WebConfigurationProperty -Filter $filterString -Name "."
if ($presentCustomField)
{
$sourceNameMatch = $customField.SourceName -eq $presentCustomField.sourceName
$sourceTypeMatch = $customField.SourceType -eq $presentCustomField.sourceType
if(-not ($sourceNameMatch -and $sourceTypeMatch))
{
$inDesiredSate = $false
}
}
else
{
$inDesiredSate = $false
}
}
return $inDesiredSate
}
#endregion
Export-ModuleMember -function *-TargetResource