From d16b6bff70430481e466c321f08cdecc36cb6da0 Mon Sep 17 00:00:00 2001 From: Richard Hauer Date: Wed, 30 Nov 2016 21:34:34 +1100 Subject: [PATCH 1/3] Overload Get-Mapping to allow search by extension only; Update Set-TargetResource to account for cases where extension exists but MIME type is different (and therefore should be updated) --- .../MSFT_xIisMimeTypeMapping.psm1 | 347 ++++++++++-------- 1 file changed, 185 insertions(+), 162 deletions(-) diff --git a/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 b/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 index 4423852fe..a5a537dfc 100644 --- a/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 +++ b/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 @@ -4,169 +4,184 @@ Import-Module -Name "$PSScriptRoot\..\Helper.psm1" # Localized messages data LocalizedData { - # culture="en-US" - ConvertFrom-StringData -StringData @' - NoWebAdministrationModule = Please ensure that WebAdministration module is installed. - AddingType = Adding MIMEType '{0}' for extension '{1}' - RemovingType = Removing MIMEType '{0}' for extension '{1}' - TypeExists = MIMEType '{0}' for extension '{1}' already exist - TypeNotPresent = MIMEType '{0}' for extension '{1}' is not present as requested - TypeStatusUnknown = MIMEType '{0}' for extension '{1}' is is an unknown status - VerboseGetTargetPresent = MIMEType is present - VerboseGetTargetAbsent = MIMEType is absent - VerboseSetTargetError = Cannot set type + # culture="en-US" + ConvertFrom-StringData -StringData @' + NoWebAdministrationModule = Please ensure that WebAdministration module is installed. + AddingType = Adding MIMEType '{0}' for extension '{1}' + UpdatingType = Updating MIMEType to '{0}' for extension '{1}' + RemovingType = Removing MIMEType '{0}' for extension '{1}' + TypeExists = MIMEType '{0}' for extension '{1}' already exist + TypeNotPresent = MIMEType '{0}' for extension '{1}' is not present as requested + TypeStatusUnknown = MIMEType '{0}' for extension '{1}' is is an unknown status + VerboseGetTargetPresent = MIMEType is present + VerboseGetTargetAbsent = MIMEType is absent + VerboseSetTargetError = Cannot set type '@ } function Get-TargetResource { - <# - .SYNOPSIS - This will return a hashtable of results - #> - [OutputType([Hashtable])] - param - ( - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [String] $Extension, - - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [String] $MimeType, - - [ValidateSet('Present', 'Absent')] - [Parameter(Mandatory)] - [String] $Ensure - ) - - # Check if WebAdministration module is present for IIS cmdlets - Assert-Module - - $mt = Get-Mapping -Extension $Extension -Type $MimeType - - if ($null -eq $mt) - { - Write-Verbose -Message $LocalizedData.VerboseGetTargetAbsent - return @{ - Ensure = 'Absent' - Extension = $null - MimeType = $null - } - } - else - { - Write-Verbose -Message $LocalizedData.VerboseGetTargetPresent - return @{ - Ensure = 'Present' - Extension = $mt.fileExtension - MimeType = $mt.mimeType - } - } + <# + .SYNOPSIS + This will return a hashtable of results + #> + [OutputType([Hashtable])] + param + ( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $Extension, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $MimeType, + + [ValidateSet('Present', 'Absent')] + [Parameter(Mandatory)] + [String] $Ensure + ) + + # Check if WebAdministration module is present for IIS cmdlets + Assert-Module + + $mt = Get-Mapping -Extension $Extension -Type $MimeType + + if ($null -eq $mt) + { + Write-Verbose -Message $LocalizedData.VerboseGetTargetAbsent + return @{ + Ensure = 'Absent' + Extension = $null + MimeType = $null + } + } + else + { + Write-Verbose -Message $LocalizedData.VerboseGetTargetPresent + return @{ + Ensure = 'Present' + Extension = $mt.fileExtension + MimeType = $mt.mimeType + } + } } function Set-TargetResource { - <# - .SYNOPSIS - This will set the desired state - #> - param - ( - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [String] $Extension, - - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [String] $MimeType, - - [ValidateSet('Present', 'Absent')] - [Parameter(Mandatory)] - [String] $Ensure - ) - - Assert-Module - - [String] $psPathRoot = 'MACHINE/WEBROOT/APPHOST' - [String] $sectionNode = 'system.webServer/staticContent' - - $mt = Get-Mapping -Extension $Extension -Type $MimeType - - if ($null -eq $mt -and $Ensure -eq 'Present') - { - # add the MimeType - Add-WebConfigurationProperty -PSPath $psPathRoot ` - -Filter $sectionNode ` - -Name '.' ` - -Value @{fileExtension="$Extension";mimeType="$MimeType"} - Write-Verbose -Message ($LocalizedData.AddingType -f $MimeType,$Extension); - } - elseif ($null -ne $mt -and $Ensure -eq 'Absent') - { - # remove the MimeType - Remove-WebConfigurationProperty -PSPath $psPathRoot ` - -Filter $sectionNode ` - -Name '.' ` - -AtElement @{fileExtension="$Extension"} - Write-Verbose -Message ($LocalizedData.RemovingType -f $MimeType,$Extension); - } - else - { - Write-Verbose -Message $LocalizedData.VerboseSetTargetError - } + <# + .SYNOPSIS + This will set the desired state + #> + param + ( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $Extension, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $MimeType, + + [ValidateSet('Present', 'Absent')] + [Parameter(Mandatory)] + [String] $Ensure + ) + + Assert-Module + + [String] $psPathRoot = 'MACHINE/WEBROOT/APPHOST' + [String] $sectionNode = 'system.webServer/staticContent' + + # $mt = Get-Mapping -Extension $Extension -Type $MimeType + $mt = Get-Mapping -Extension $Extension + + if ($Ensure -eq 'Present') + { + if ( ! $mt ) + { + # add the MimeType + Add-WebConfigurationProperty -PSPath $psPathRoot ` + -Filter $sectionNode ` + -Name '.' ` + -Value @{fileExtension="$Extension";mimeType="$MimeType"} + Write-Verbose -Message ($LocalizedData.AddingType -f $MimeType,$Extension); + } + else + { + # update the MimeType + Set-WebConfigurationProperty -PSPath $psPathRoot ` + -Filter "$sectionNode/mimeMap[@fileExtension='$Extension']" ` + -Name '.' ` + -Value @{fileExtension="$Extension";mimeType="$MimeType"} + Write-Verbose -Message ($LocalizedData.UpdatingType -f $MimeType,$Extension); + } + + } + elseif ($null -ne $mt -and $Ensure -eq 'Absent') + { + # remove the MimeType + Remove-WebConfigurationProperty -PSPath $psPathRoot ` + -Filter $sectionNode ` + -Name '.' ` + -AtElement @{fileExtension="$Extension"} + Write-Verbose -Message ($LocalizedData.RemovingType -f $MimeType,$Extension); + } + else + { + Write-Verbose -Message $LocalizedData.VerboseSetTargetError + } } function Test-TargetResource { - <# - .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 - #> - - [OutputType([System.Boolean])] - param - ( - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [String] $Extension, - - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [String] $MimeType, - - [ValidateSet('Present', 'Absent')] - [Parameter(Mandatory)] - [String] $Ensure - ) - - [Boolean] $DesiredConfigurationMatch = $true; - - Assert-Module - - $mt = Get-Mapping -Extension $Extension -Type $MimeType - - if (($null -eq $mt -and $Ensure -eq 'Present') -or ($null -ne $mt -and $Ensure -eq 'Absent')) - { - $DesiredConfigurationMatch = $false; - } - elseif ($null -ne $mt -and $Ensure -eq 'Present') - { - # Already there - Write-Verbose -Message ($LocalizedData.TypeExists -f $MimeType,$Extension); - } - elseif ($null -eq $mt -and $Ensure -eq 'Absent') - { - # TypeNotPresent - Write-Verbose -Message ($LocalizedData.TypeNotPresent -f $MimeType,$Extension); - } - else - { - $DesiredConfigurationMatch = $false; - Write-Verbose -Message ($LocalizedData.TypeStatusUnknown -f $MimeType,$Extension); - } - - return $DesiredConfigurationMatch + <# + .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 + #> + + [OutputType([System.Boolean])] + param + ( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $Extension, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $MimeType, + + [ValidateSet('Present', 'Absent')] + [Parameter(Mandatory)] + [String] $Ensure + ) + + [Boolean] $DesiredConfigurationMatch = $true; + + Assert-Module + + $mt = Get-Mapping -Extension $Extension -Type $MimeType + + if (($null -eq $mt -and $Ensure -eq 'Present') -or ($null -ne $mt -and $Ensure -eq 'Absent')) + { + $DesiredConfigurationMatch = $false; + } + elseif ($null -ne $mt -and $Ensure -eq 'Present') + { + # Already there + Write-Verbose -Message ($LocalizedData.TypeExists -f $MimeType,$Extension); + } + elseif ($null -eq $mt -and $Ensure -eq 'Absent') + { + # TypeNotPresent + Write-Verbose -Message ($LocalizedData.TypeNotPresent -f $MimeType,$Extension); + } + else + { + $DesiredConfigurationMatch = $false; + Write-Verbose -Message ($LocalizedData.TypeStatusUnknown -f $MimeType,$Extension); + } + + return $DesiredConfigurationMatch } #region Helper Functions @@ -174,17 +189,25 @@ function Test-TargetResource function Get-Mapping { - [CmdletBinding()] - param - ( - [String] $Extension, - - [String] $Type - ) - - [String] $filter = "system.webServer/staticContent/mimeMap[@fileExtension='" + ` - $Extension + "' and @mimeType='" + $Type + "']" - return Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter $filter -Name . + [CmdletBinding()] + param + ( + [String] $Extension, + + [String] $Type = $null + ) + + [String] $filter = "system.webServer/staticContent/mimeMap" + if ( $Type ) + { + $filter += "[@fileExtension='$Extension' and @mimeType='$Type']" + } + else + { + $filter += "[@fileExtension='$Extension']" + } + + return Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter $filter -Name . } #endregion From 4c9761e6f4fb82fe4d3a8688bf1885048b692f06 Mon Sep 17 00:00:00 2001 From: Richard Hauer Date: Wed, 30 Nov 2016 21:36:12 +1100 Subject: [PATCH 2/3] Update whitespace for consistency. --- .../MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 b/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 index a5a537dfc..01f20859b 100644 --- a/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 +++ b/DSCResources/MSFT_xIisMimeTypeMapping/MSFT_xIisMimeTypeMapping.psm1 @@ -8,7 +8,7 @@ data LocalizedData ConvertFrom-StringData -StringData @' NoWebAdministrationModule = Please ensure that WebAdministration module is installed. AddingType = Adding MIMEType '{0}' for extension '{1}' - UpdatingType = Updating MIMEType to '{0}' for extension '{1}' + UpdatingType = Updating MIMEType to '{0}' for extension '{1}' RemovingType = Removing MIMEType '{0}' for extension '{1}' TypeExists = MIMEType '{0}' for extension '{1}' already exist TypeNotPresent = MIMEType '{0}' for extension '{1}' is not present as requested From 83bb9c5229e8576a64fa6b1dc3413b7ea566cbf8 Mon Sep 17 00:00:00 2001 From: Richard Hauer Date: Wed, 30 Nov 2016 22:18:23 +1100 Subject: [PATCH 3/3] Add project wrapper for VS --- xWebAdministration.pssproj | 137 ++++++++++++++++++++++++++++++++ xWebAdministration.pssproj.user | 6 ++ 2 files changed, 143 insertions(+) create mode 100644 xWebAdministration.pssproj create mode 100644 xWebAdministration.pssproj.user diff --git a/xWebAdministration.pssproj b/xWebAdministration.pssproj new file mode 100644 index 000000000..47027ca5a --- /dev/null +++ b/xWebAdministration.pssproj @@ -0,0 +1,137 @@ + + + + Debug + 2.0 + 6CAFC0C6-A428-4d30-A9F9-700E829FEA51 + Exe + MyApplication + MyApplication + xWebAdministration + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xWebAdministration.pssproj.user b/xWebAdministration.pssproj.user new file mode 100644 index 000000000..5283ef199 --- /dev/null +++ b/xWebAdministration.pssproj.user @@ -0,0 +1,6 @@ + + + + ShowAllFiles + + \ No newline at end of file