简体   繁体   English

创建 PowerShell 脚本以启用 HTTPCompression 并在 web 站点的配置文件中使用命令行(appcmd)添加 MimeType

[英]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.

相关问题 使用appcmd添加新的网站问题 - using appcmd to add new web site issue 如何使用appcmd.exe在默认网站中删除虚拟目录? - How can I remove a virtual directory in the Default Web Site using appcmd.exe? 是否可以在命令行或通过配置文件为“InternalsVisibleTo”添加包? - Is it possible to add packages for "InternalsVisibleTo" at the command line or via a config file? 从配置文件启用 Serilog 的 Selflog - Enable Serilog's Selflog from config file 不使用命令行工具加密web.config - Encrypt web.config without command line tool 如何在带有配置文件的Powershell脚本中使用自定义WCF代理? - How to use a custom WCF proxy in a Powershell script with a config file? 使用MSBuild,如何从命令行构建MVC4解决方案(在此过程中应用Web.config转换)并输出到文件夹? - Using MSBuild, how do I build an MVC4 solution from the command line (applying Web.config transformations in the process) and output to a folder? 有没有一种简单的方法可以仅通过使用它的路径来读取web.config文件的appsettings? - Is there an easy way to read the appsettings of a web.config file just by using it's path? 使用链接文件的 App 或 Web Config 中的 AppSettings - AppSettings in App or Web Config Using a Linked File 如何在 Visual Studio Web 2013 中添加 app.config 或 web.config 文件? - How can I add an app.config or web.config file in Visual Studio Web 2013?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM