1

我需要使用 powershell 在尚未将代码部署到文件系统的 WebApplications 上配置 IIS7.5(可能根本没有,可能存在旧的/损坏的 web.configs)。我希望能够在 APPHOST 级别完成这一切。(注意底部关于 Powershell > AppCmd 的使用)。

我可以正确设置所有值,但是,有点勤奋,我还想通过在设置后检索它们来验证这些值是否正确设置。

场景如下:我可以使用 AppCmd 设置此值,以便使用 /Commit:APPHOST 标志在 APHOST 级别应用该设置。但是,我还没有找到专门在 APPHOST 级别读取值的方法。

设置代码成功:

C:\Windows\System32\inetsrv\appcmd.exe set config "webSiteName/webAppName" -section:system.webServer/security/authentication/anonymousAuthentication /enabled:"True" /commit:apphost

但是,我找不到使用 AppCmd(或 Powershell)读取值的方法:运行以下 AppCmd 会返回错误,因为文件夹中预先存在的 web.config 已损坏(具体错误并不重要,因为它正在读取WebApp 的 web.config 而不是 ApplicationHost.config/APPHOST):

C:\Windows\System32\inetsrv\appcmd.exe list config "MACHINE/WEBROOT/APPHOST/webSiteName/webAppName" -section:system.webServer/security/authentication/anonymousAuthentication
ERROR ( message:Configuration error
Filename: \\?\c:\inetpub\wwwroot\webSiteName\webAppName\web.config
Line Number: 254
Description: The configuration section 'system.runtime.caching' cannot be read because it is missing a section declaration
. )

注意:我宁愿在 Powershell 中完成这一切,而不是使用 AppCmd,所以如果有人有修改 WebApplication 的 anonymousAuthentication 部分的 APPHOST 设置的语法,它位于网站下,从 Powershell 内部(Get-WebConfiguration 似乎只使用 WebApp web.config),那将是非常棒的,非常感谢!

4

1 回答 1

4

以下是如何在 PowerShell 中执行此操作:

[Reflection.Assembly]::Load(
"Microsoft.Web.Administration, Version=7.0.0.0, 
Culture=Neutral, PublicKeyToken=31bf3856ad364e35") > $null

$serverManager = New-Object Microsoft.Web.Administration.ServerManager
$config = $serverManager.GetApplicationHostConfiguration()
$anonymousAuthenticationSection = $config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "simpleasp.net")
Write-Host "Current value: " $anonymousAuthenticationSection["enabled"]

# Now set new value
$anonymousAuthenticationSection["enabled"] = $true

$serverManager.CommitChanges()
于 2012-01-26T00:46:24.020 回答