Last active
November 14, 2017 11:45
-
-
Save GiscardBiamby/6156947 to your computer and use it in GitHub Desktop.
PowerShell snippet for configuring <system.webServer><httpCompression>. Using this in the startup script for an Azure Cloud Service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[xml]$config = (& "$env:WINDIR\system32\inetsrv\appcmd.exe" list config /section:httpCompression) | |
if ($config -ne $null -and ($config.GetElementsByTagName("httpCompression") -ne $null)) { | |
$compression = $config.GetElementsByTagName("httpCompression")[0] | |
$dynamic = $compression.dynamicTypes | |
EnableCompression $dynamic "dynamicTypes" "application/x-javascript" | |
EnableCompression $dynamic "dynamicTypes" "application/atom+xml" | |
} | |
function EnableCompression([System.Xml.XmlElement]$node, [string]$nodeName, [string]$mimeType) { | |
$appCmdMimeType = $mimeType.Replace("+", "%u002b") | |
if (Is-CompressionSet $node $mimeType) { | |
Write-Host "Compression setting for $nodeName::'$mimeType' ($appCmdMimeType) exists, clearing it..." | |
& "$env:WINDIR\system32\inetsrv\appcmd.exe" set config -section:system.webServer/httpCompression /-"$nodeName.[mimeType='$appCmdMimeType']" /commit:appHost; | |
} | |
else { | |
Write-Host "Compression for $nodeName::'$mimeType' ($appCmdMimeType) was not already set. Didn't need to clear the setting." | |
} | |
& "$env:WINDIR\system32\inetsrv\appcmd.exe" set config -section:system.webServer/httpCompression /+"$nodeName.[mimeType='$appCmdMimeType',enabled='True']" /commit:apphost; | |
} | |
function Is-CompressionSet([System.Xml.XmlElement]$node, [string]$mimeType) { | |
return ` | |
($node -ne $null) ` | |
-and ($node.SelectSingleNode("add[@mimeType='$mimeType']") -ne $null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lifesaver! Thanks very much for this 😀
edit: as a follow up, I found this blog which describes a PowerShell script generation feature that went in with IIS 8. Works like a charm!