powershell - 使用AppCmd仅在APPHOST中列出配置

标签 powershell iis-7 iis-7.5 appcmd

我需要使用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/

相关文章:

asp.net - 在某些报告下,aspx Crystal Report Viewer 会在 IIS 7.5 上无限期挂起的可能原因?

powershell - 从超过1级深度的属性中从数组中提取项目?

powershell 比较两个数组,创建只有不匹配值的第三个数组

Powershell COM+ 设置

asp.net - 为什么我的 ASP.NET 4.0 Web 服务因 NetworkService 编译错误而失败,但 LocalSystem 成功

asp.net - Windows 2008 Server x64 上的 IIS 7.5 ASP 32 位应用程序池上为 500.0 0x800700c1

authorization - 授予对 IIS 7.5 ApplicationPoolIdentity 的访问权限

c# - 为什么某些属性和方法在 Powershell 中可用,但在 C# 中不可用

xml - 配置文件不是格式正确的 XML #2

asp.net - IIS禁止直接访问图像