PowerShell 管理单元:更改配置节中的简单设置
作者:Thomas Deml
在前面的演练中,你学习了如何管理 IIS 命名空间容器,例如站点、应用程序池、应用程序和虚拟目录。
在本演练中,我们将管理未通过 IIS 命名空间公开的配置设置。
介绍
可通过多个 cmdlet 更改无法通过 IIS 命名空间配置的 IIS 设置,无法配置是指无法使用内置 cmdlet 对它们进行修改。 必须转而使用 IIS 提供的 cmdlet。 本演练依赖于在前面的演练中创建的站点、应用程序和虚拟目录。
Get-WebConfiguration 和 Get-WebConfigurationProperty
通过 Get-WebConfiguration 和 Get-WebConfigurationProperty,可以获取 IIS 配置节。 它们与 Get-Item 和 Get-ItemProperty 很相似。 其中,Get-Item* 仅适用于命名空间容器(Sites、Apps、AppPools、VDirs)Get-WebConfiguration* 将适用于任何 IIS 配置节。
查询配置设置
让我们看看在之前创建的 DemoApp 应用程序上启用了 directoryBrowse 节的哪些设置。 首先导航到 DemoApp 文件夹,然后查询此文件夹中的身份验证设置。 下面是操作方法:
PS IIS:\> cd IIS:\Sites\DemoSite\DemoApp
PS IIS:\Sites\DemoSite\DemoApp> dir
Type Name Physical Path
---- ---- -------------
file Default.htm C:\DemoSite\DemoApp\Default.htm
virtualDirectory DemoVirtualDir2 C:\DemoSite\DemoVirtualDir2
在下一个示例中,我们使用 -filter 参数来指定要关注的配置节,使用 -name 参数来指定要查看的属性。 如果想要查看不是当前位置的节的设置,可以使用其顶部的 -PSPath 属性。 以下示例展示了如何在默认网站上查询目录浏览设置:
Get-WebConfigurationProperty -filter /system.webServer/directoryBrowse -name enabled -PSPath 'IIS:\Sites\Default Web Site'
ItemXPath : /system.webServer/directoryBrowse
IsInheritedFromDefaultValue : False
IsProtected : False
Name : enabled
TypeName : System.Boolean
Schema : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value : False
IsExtended : False
使用 Set-WebConfigurationProperty
更改设置非常简单:
处理锁定的节
这里有一个问题。 身份验证节通常是锁定的,也就是说无法将它们写入 Web.config 文件,但必须转而将它们写入中央 applicationhost.config 文件。 如果使用上述命令启用 WindowsAuthentication,操作将失败并出现锁定冲突:
PS IIS:\Sites\DemoSite\DemoApp> Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true
Set-WebConfigurationProperty : This configuration section cannot be used at this path. This happens
when the section is locked at a parent level. Locking is either by default (overrideModeDefault="D
eny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="fa
lse".
At line:1 char:29
+ Set-WebConfigurationProperty <<<< -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true
这里要做的是使用 -PSPath 和 -location 参数。 以下命令将为 DemoApp 应用程序启用 Windows 身份验证。 配置会写入 applicationhost.config,不过是使用位置标记写入的。 单击此处,查找有关锁定和位置标记的详细信息。
PS IIS:\Sites\DemoSite\DemoApp> Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath IIS:\ -location DemoSite/DemoApp
但是,在查询配置时不需要指定位置。 常规 Get-WebConfigurationProperty 命令将显示该设置已启用。
PS IIS:\Sites\DemoSite\DemoApp> Get-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled
True
Get-WebConfiguration 与Get-WebConfigurationProperty
这也适用于上一个示例中的 Get-Item 和Get-ItemProperty。 Get-WebConfiguration 获取的是整个节,而不是只获取属性。 这样,可以将节存储到变量,对节属性进行修改,并通过 Set-WebConfiguration 将节保存回来。 获得命令补全的优势。
以下是一个示例。 不要只是复制粘贴。 浏览 windowsAuthentication 节的属性。 键入 $winAuth. 并点击 <Tab> 键来遍历可用的属性和函数。
PS IIS:\Sites\DemoSite\DemoApp> $winAuth = Get-WebConfiguration -filter /system.webServer/security/authentication/windowsAuthentication
PS IIS:\Sites\DemoSite\DemoApp> $winAuth.enabled = $false
PS IIS:\Sites\DemoSite\DemoApp> $winAuth | set-Webconfiguration -filter /system.webServer/security/authentication/windowsAuthentication -PSPath IIS:\ -location "DemoSite/DemoApp"
Add-WebConfiguration
Add-WebConfiguration 是一个 cmdlet。如果必须向 IIS 配置集合添加一些内容,则需要使用它。 处理程序、模块、默认文档设置,还是 IIS 使用集合来存储多个值的其他一些示例。
下面的示例展示了如何将新的默认文档添加到 DemoApp 默认文档集合:
PS IIS:\Sites\DemoSite\DemoApp>Add-WebConfiguration /system.webServer/defaultDocument/files "IIS:\sites\Default Web Site" -at 0 -value
@{value="new-index.html"}
此示例使用附加参数 -at。 这让你能够指定要在集合中的哪里添加新值。 0 表示添加到开头;-1 指定末尾。
总结
在本演练中,你学习了如何使用 IIS 提供的 Web 配置 cmdlet。 你学习了如何查询配置设置、如何使用位置标记配置设置、如何利用命令行补全以及如何向集合添加条目。
在下一个演练中,你将了解如何使用高级 IIS 提供程序功能(例如 globbing 和 XPath)完成一些复杂的配置任务。