[英]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.我想启用 httpcompression,然后使用 appcmd 将 mimetypes 添加到 web.config 文件中。 I know we can do that from
我知道我们可以从
applicantionHost.config
申请主机.config
file.文件。 It is enabled by default above IIS7.5
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但我的要求是使用 appcmd 启用并将 mime 类型直接添加到 web.config 文件(基本上覆盖 applicationHost.config 中存在的设置)
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:要添加 MIME 类型,请使用以下语法:
appcmd set config /section:staticContent /+"[fileExtension='string',mimeType='string']"
The variable fileExtension string is a file name extension.变量fileExtension字符串是文件扩展名。 The variable mimeType string is a MIME type.
变量mimeType字符串是一种 MIME 类型。 For example, to create a MIME type, type the following at the command prompt, and then press ENTER:
例如,若要创建 MIME 类型,请在命令提示符处键入以下内容,然后按 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 .有关添加 MIME 类型的更多信息,您可以参考此链接: 添加 MIME 类型。
$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我们正在使用
commit:app
在应用程序的 web.config 中添加配置条目 您可以使用/commit:apphost
在位于的全局 apphost 文件中添加相同的
%windir%\System32\inetsrv\config\applicationHost.config
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.