Skip to content

Instantly share code, notes, and snippets.

@kittholland
Created January 5, 2017 22:41
Show Gist options
  • Save kittholland/730666229ec9cb7be915768e7d5c9be8 to your computer and use it in GitHub Desktop.
Save kittholland/730666229ec9cb7be915768e7d5c9be8 to your computer and use it in GitHub Desktop.
enum SourceType
{
RequestHeader = 0
ResponseHeader = 1
ServerVariable = 2
}
enum Ensure
{
Absent
Present
}
[DscResource()]
class IISCustomLogging
{
[DscProperty(Key)]
[string]
$LogFieldName
[DscProperty(Mandatory)]
[string]
$SourceName
[DscProperty(Mandatory)]
[SourceType]
$SourceType
[DscProperty(Key)]
[string]
$Site
[DscProperty()]
[Ensure]
$Ensure
[IISCustomLogging] Get()
{
Write-Verbose -Message "Importing WebAdministration Module"
Import-Module WebAdministration -ErrorAction Stop
$filterString = "/system.applicationHost/sites/site[@name='{0}']/logFile/customFields/add[@logFieldName='{1}']" -f $this.Site, $this.LogFieldName
Write-Verbose -Message "Generated xPath filter: $filterString"
$item = Get-WebConfigurationProperty -Filter $filterString -Name "."
If($item)
{
Write-Verbose -Message "Found logFieldName $($this.LogFieldName), retrieving properties"
$sourceNameMatch = $this.SourceName -eq $item.sourceName
$sourceTypeMatch = $this.SourceType -eq $item.sourceType
If($sourceNameMatch -and $sourceTypeMatch)
{
Write-Verbose -Message "Properties match desired state, item is present"
$this.Ensure = 'Present'
}
Else
{
Write-Verbose -Message "Properties do not match desired state, item is absent"
$this.Ensure = 'Absent'
}
$this.SourceName = $item.sourceName
$this.SourceType = $item.sourceType
}
Else
{
Write-Verbose -Message "No customFields found for $($this.LogFieldName)"
$this.Ensure = 'Absent'
}
return $this
}
[bool] Test()
{
Write-Verbose -Message "Importing WebAdministration Module"
Import-Module WebAdministration -ErrorAction Stop
$filterString = "/system.applicationHost/sites/site[@name='{0}']/logFile/customFields/add[@logFieldName='{1}']" -f $this.Site, $this.LogFieldName
Write-Verbose -Message "Generated xPath filter: $filterString"
$item = Get-WebConfigurationProperty -Filter $filterString -Name "."
If($item)
{
Write-Verbose -Message "Found logFieldName $($this.LogFieldName), retrieving properties"
If($this.Ensure -eq 'Absent')
{
return $false
}
$sourceNameMatch = $this.SourceName -eq $item.sourceName
$sourceTypeMatch = $this.SourceType -eq $item.sourceType
If($sourceNameMatch -and $sourceTypeMatch)
{
Write-Verbose -Message "Properties match desired state"
return $true
}
Else
{
Write-Verbose -Message "Properties do not match desired state, item is absent"
return $false
}
}
Else
{
Write-Verbose -Message "LogFieldName $($this.LogFieldName) not present"
If($this.Ensure -eq 'Present')
{
return $false
}
Else
{
return $true
}
}
}
[void] Set()
{
Write-Verbose -Message "Importing WebAdministration Module"
Import-Module WebAdministration -ErrorAction Stop
$filterString = "/system.applicationHost/sites/site[@name='{0}']/logFile/customFields/add[@logFieldName='{1}']" -f $this.Site, $this.LogFieldName
Write-Verbose -Message "Generated xPath filter: $filterString"
$item = Get-WebConfigurationProperty -Filter $filterString -Name "."
If($item)
{
Write-Verbose -Message "Clearing configuration for $($this.LogFieldName)."
Clear-WebConfiguration -Filter $filterString
If($this.Ensure -eq 'Absent')
{
return
}
}
Write-Verbose -Message "Adding configuration for $($this.LogFieldName)"
$addFilterString = "/system.applicationHost/sites/site[@name='{0}']/logFile/customFields" -f $this.Site
$addHashTable = @{
logFieldName = $this.LogFieldName
sourceName = $this.SourceName
sourceType = $this.SourceType
}
Write-Verbose -Message "Setting logFieldName to $($this.LogFieldName)"
Write-Verbose -Message "Setting sourceName to $($this.SourceName)"
Write-Verbose -Message "Setting sourceType to $($this.SourceType)"
Add-WebConfigurationProperty -Filter $addFilterString -Name "." -Value $addHashTable
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment