powershell - 如何使用 powershell 设置 IIS 的表单例份验证(system.web/authentication)的 overrideMode?

标签 powershell iis web-administration

我需要使用 powershell 自动配置新的 IIS 服务器,并且需要更改表单例份验证功能的功能委派设置(其 overrideMode)。

我基本上希望更改使用此命令时看到的 overrideMode:

Get-WebConfiguration -Filter //system.web/authentication -PSPath 'MACHINE/WEBROOT/APPHOST' | fl *

我可以使用 Set-WebConfiguration 为其他类型的身份验证方法设置它,例如我会做的 Windows 身份验证:
Set-WebConfiguration -Filter //System.webServer/Security/Authentication/windowsAuthentication -PSPath 'MACHINE/WEBROOT/APPHOST' -Metadata overrideMode -Value Allow

但由于某种原因,我不能对//system.web/authentication 做同样的事情,我不明白为什么。当我尝试时,我收到此错误:
PS H:\> Set-WebConfiguration -Filter //System.web/Authentication -PSPath 'MACHINE/WEBROOT/APPHOST' -Metadata overrideMode -Value Allow
Set-WebConfiguration : Filename: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config
Line number: 974
Error: 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="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".
At line:1 char:1
+ Set-WebConfiguration -Filter //System.web/Authentication -PSPath 'MAC ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-WebConfiguration], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.IIs.PowerShell.Provider.SetConfigurationCommand

我做错了什么,如何更改此值?还有一些其他的委托(delegate)功能似乎也相同。

这适用于 Windows Server 2016 上的 IIS 10

编辑:我注意到,当我使用 IIS 管理器(而不是 Powershell)将委派设置更改为只读时,它会通过将其添加到 applicationHost.config 文件中具有 overrideMode="Deny"的位置来“锁定”该功能。完成此操作后,如果我尝试将其更改回允许使用 Powershell,则会收到错误消息。这就是问题。如果我只使用 powershell 将其设置为允许或拒绝,它不会给出错误,但更改不会反射(reflect)在 IIS 管理器中。似乎对于某些委派权限,IIS 管理器使用与 Powershell 更改 overrideMode 不同的方法,一旦使用 UI 锁定它,就无法使用 Powershell 解锁它。

最佳答案

您可以检查以确保从这篇文章中启用了这些 Windows 功能,因为这解决了那里的某些错误:

Config Error: This configuration section cannot be used at this path

您是否可以在文件上设置属性使其不是只读的?

# You may need to use the -Credential parameter for this to work on a remote machine
Set-ItemProperty \\?\C:\Windows\system32\inetsrv\config\applicationHost.config -name IsReadOnly -value $false

然后尝试重新运行您的 Set-WebConfiguration命令。

您也可以尝试将该配置文件作为 XML 加载到 PowerShell 中,然后修改 overrideMode属性并将其保存回 IIS 路径。
[xml]$XmlDoc = Get-Content -Path '\\?\C:\Windows\system32\inetsrv\config\applicationHost.config'
# '//System.Web/Authentication' is the XPath provided above
$AuthNode = $XMLdoc.SelectSingleNode('//System.Web/Authentication')
$AuthNode.overrideMode = "Allow"
$XmlDoc.Save("\\?\C:\Windows\system32\inetsrv\config\applicationHost.config")

我没有在这台机器上设置 IIS,但假设它是 XML 中的有效 XPATH,您应该能够更改标志,假设当您尝试更新文件时没有使用该文件。

关于powershell - 如何使用 powershell 设置 IIS 的表单例份验证(system.web/authentication)的 overrideMode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52282528/

相关文章:

iis - 我可以使用 Powershell 在远程 IIS 服务器上创建站点和应用程序池吗?

powershell - Import-Module WebAdministration 不会从脚本加载,而是从命令行加载

powershell - 执行 invoke-webrequest 以获取 Powershell 中多个网站的状态

powershell - 如何获取 64 位的 pshome 路径?

powershell - 如何正确处理通过SendTo快捷方式作为参数传递给PowerShell脚本的文件名中的空格?

c# - ASP.NET UDP 套接字代码适用于开发,但不适用于 IIS 的生产

wcf - 想要将 WCF Web 服务作为 Windows 服务托管,而不是在 IIS 中托管

powershell - 从计算机商店删除证书

powershell - 使用 TFS vNext 任务 : A parameter cannot be found that matches parameter name 的问题

jquery - ERR_SPDY_PROTOCOL_ERROR Ajax .net Web API