Shashank Sood
Shashank Sood

Reputation: 480

Create PowerShell script to enable HTTPCompression and add MimeTypes using command line(appcmd) in a web site's config file

I want to enable httpcompression and then add mimetypes to web.config files usng appcmd. I know we can do that from

applicantionHost.config

file. It is enabled by default above IIS7.5

We can verify that from

%windir%\System32\inetsrv\config\applicationHost.config

But what my requirements is to enable and add mime types directly to web.config file(basically override the settings existing in applicationHost.config) using appcmd

Upvotes: 0

Views: 557

Answers (2)

Shashank Sood
Shashank Sood

Reputation: 480

$appcmdpath="$env:windir\system32\inetsrv\appcmd.exe"
$path="{PathofHostedAppFromIIS}/"

ECHO 'Remove Existing sections if any this is important if we face issues any issue while it for the first time or running the script mutiple times incase of any error'
& $appcmdpath clear config $path -section:system.webServer/httpCompression /delete:true /commit:app
& $appcmdpath clear config $path -section:system.webServer/urlCompression /delete:true /commit:app

ECHO 'Enable Http Compression'
& $appcmdpath set config $path -section:system.webServer/httpCompression /directory:'%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files' /commit:app

ECHO 'Clear apphost default compression'
& $appcmdpath set config $path -section:system.webServer/httpCompression /~"staticTypes" /commit:app

ECHO 'Add default stypes available in Apphost'
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='text/*',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='message/*',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/javascript',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/atom+xml',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/xaml+xml',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='image/svg+xml',enabled='True']" /commit:app

ECHO 'Below script can be added to exclude mime types '
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='image/jpeg',enabled='False']" /commit:app

& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='*/*',enabled='False']" /commit:app

ECHO 'Enable URL Compression'
& $appcmdpath set config $path -section:system.webServer/urlCompression /doDynamicCompression:"true"  /doStaticCompression:"true" /commit:app

We are using commit:app to add the config entries in application's web.config You can use /commit:apphost to add the same at global apphost file located at

%windir%\System32\inetsrv\config\applicationHost.config

Upvotes: 0

samwu
samwu

Reputation: 5215

You could enable and disable compression for site using below command:

appcmd set config "site1" /section:urlCompression /doDynamicCompression:True

appcmd set config "urlsample" /section:urlCompression /doStaticCompression:True

To add a MIME type, use the following syntax:

appcmd set config /section:staticContent /+"[fileExtension='string',mimeType='string']"

The variable fileExtension string is a file name extension. The variable mimeType string is a MIME type. For example, to create a MIME type, type the following at the command prompt, and then press ENTER:

appcmd set config /section:staticContent /+"[fileExtension='.xyz',mimeType='application/octet-stream']"

More information about adding a MIME type you can refer to this link: To add a MIME type.

Upvotes: 1

Related Questions