Skip to content

Instantly share code, notes, and snippets.

@GiscardBiamby
Last active November 14, 2017 11:45
Show Gist options
  • Save GiscardBiamby/6156947 to your computer and use it in GitHub Desktop.
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.
[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);
}
@Hinni
Copy link

Hinni commented Aug 4, 2017

Thank you! Great script!
I use it with octopus as a deployment step :-)

@jlucktay
Copy link

jlucktay commented Sep 1, 2017

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment