我需要使用Powershell在尚未将代码部署到文件系统上的Web应用程序上配置IIS7.5(可能根本存在,可能存在旧的/损坏的web.config)。我希望能够在APPHOST级别上完成所有这些操作。 (在底部注意有关Powershell> AppCmd的使用)。
我可以正确设置所有值,但是,要勤奋一点,我还想通过在设置后检索它们来验证是否正确设置了这些值。
这是场景:
我可以使用AppCmd设置此值,以便使用/ Commit:APPHOST标志在APPHOST级别应用设置。但是,我还没有找到一种专门在APPHOST级别读取值的方法。
设置代码成功:
C:\Windows\System32\inetsrv\appcmd.exe set config "webSiteName/webAppName" -section:system.webServer/security/authentication/anonymousAuthentication /enabled:"True" /commit:apphost
但是,我找不到使用AppCmd(或Powershell)读取值的方法:
由于文件夹中预先存在的web.config损坏,运行以下AppCmd将返回错误(具体错误不重要,因为它正在读取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来完成所有操作,因此,如果任何人都具有从Powershell内部修改Web应用程序(位于网站下)的onymousAuthenticationAuthentication部分的APPHOST设置的语法(Get-WebConfiguration似乎仅使用WebApp web.config),那就太棒了,非常感谢!
最佳答案
这是在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()
关于powershell - 使用AppCmd仅在APPHOST中列出配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9005849/