PowerShell 管理单元:高级配置任务

作者:Thomas Deml

在本演练中,你将了解如何使用 XPath 查询和通配符完成一些高级配置任务。

介绍

上一个演练介绍了 *-WebConfiguration 和 *-WebConfigurationProperty cmdlet。 这些 cmdlet 的功能远不止表面上看到的那样。 -filter 参数不仅仅是指定配置节的一种方法。 它还是 XPath 查询,在本演练中,我们将探讨如何利用该参数。 还有一些有用的方法可以将通配符与 *-WebConfiguration* 命令配合使用。

本演练使用在前面的示例中创建的站点、应用程序和虚拟目录。

使用 XPath 查询

下面是一个简单的示例,演示如何将 wilcard 与 Get-WebConfigurationProperty cmdlet 配合使用:

PS IIS:\Sites\DemoSite\DemoApp> Get-WebConfigurationProperty -filter //defaultDocument/files -name Collection[value="index*"] | select value

还有一个示例。 下面是由 ASPNET_ISAPI.DLL 执行的所有处理程序映射:

PS IIS:\Sites\DemoSite\DemoApp> Get-WebConfigurationProperty -filter //handlers -name Collection[scriptProcessor="*aspnet_isapi.dll"]  | select name,path
name                                              path
----                                              ----
svc-ISAPI-2.0-64                                  *.svc
svc-ISAPI-2.0                                     *.svc
AXD-ISAPI-2.0                                     *.axd
PageHandlerFactory-ISAPI-2.0                      *.mspx
SimpleHandlerFactory-ISAPI-2.0                    *.ashx
WebServiceHandlerFactory-ISAPI-2.0                *.asmx
HttpRemotingHandlerFactory-rem-ISAPI-2.0          *.rem
HttpRemotingHandlerFactory-soap-ISAPI-2.0         *.soap
AXD-ISAPI-2.0-64                                  *.axd
PageHandlerFactory-ISAPI-2.0-64                   *.mspx
SimpleHandlerFactory-ISAPI-2.0-64                 *.ashx
WebServiceHandlerFactory-ISAPI-2.0-64             *.asmx
HttpRemotingHandlerFactory-rem-ISAPI-2.0-64       *.rem
HttpRemotingHandlerFactory-soap-ISAPI-2.0-64      *.soap

假设你不太喜欢 ASP.Net 文件的 .aspx 扩展名,并且希望将所有 IIS 处理程序映射从 *.aspx 更改为 *.mspx。 可能会更短吗?

PS IIS:\Sites\DemoSite\DemoApp> set-webconfiguration "/system.webServer/handlers/add[@path='*.aspx']/@path" -value "*.mspx"

让我们看看是否已设置更改:

(get-webconfiguration //handlers).collection | select name,path

现在来看看配置文件本身。 可以使用在上一演练中了解的 get-item cmdlet。

PS IIS:\Sites\DemoSite\DemoApp> get-content (((get-item .).physicalPath).ToString() + "\web.config")
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
         <handlers>
            <remove name="PageHandlerFactory-ISAPI-2.0-64" />
            <remove name="PageHandlerFactory-ISAPI-2.0" />
            <remove name="PageHandlerFactory-Integrated" />
            <add name="PageHandlerFactory-Integrated" path="*.mspx" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.PageHandlerFactory" preCondition="integratedMode" />
            <add name="PageHandlerFactory-ISAPI-2.0" path="*.mspx" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv2.0,bitness32" responseBufferLimit="0" />
            <add name="PageHandlerFactory-ISAPI-2.0-64" path="*.mspx" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv2.0,bitness64" responseBufferLimit="0" />
        </handlers>
    </system.webServer>
</configuration>

可以看到配置系统已删除了旧处理程序,并将其替换为现在映射到 *.mspx 的新处理程序。

发现 IIS 配置

如果你知道要配置的内容,就再好不过了。 但是,如果你不知道会怎么样? 下面提供了一些帮助。

显示可用的 IIS 配置节

get-webconfiguration //* | where {$_.psbase.SectionPath -like "*" -and $_.psbase.SectionPath.length -gt 0} | select SectionPath

显示可在特定节上配置的属性:

get-webconfiguration system.webServer/caching | select -exp Attributes | select Name

将两者放在一起,即显示所有节及其属性。

get-webconfiguration //* | where {$_.psbase.SectionPath -like "*" -and $_.psbase.SectionPath.length -gt 0} | foreach {$_.SectionPath.ToUpper();get-webconfiguration $_.SectionPath | select -exp Attributes | select Name;"`n"} | more

我们可能会在稍后的技术预览版中将上述命令打包到一些函数中,但你现在已经获得了 :)。

总结

在本演练中,你学习了如何使用通配符和 XPath 查询完成复杂的 IIS 配置任务。 下一个演练将介绍如何发现状态和运行时数据。