gpt4 book ai didi

iis-7 - 如何在 PowerShell 中迭代应用程序 IIS7 的身份验证

转载 作者:行者123 更新时间:2023-12-02 17:27:08 27 4
gpt4 key购买 nike

我需要迭代 IIS 应用程序的所有身份验证模式并禁用除一种之外的所有模式。

类似于:

foreach($itm in [collection of authentication modes for app]){
if([certain authentication]){enabled = true}else{enabled = false}}

我熟悉 Set-WebConfigurationProperty。

最佳答案

您可以通过调用 Get-WebConfiguration 来迭代给定站点的根 Web 应用程序的所有 native (以及任何已安装的第三方)身份验证模式:

$siteName = "MySiteName"

$authentications = Get-WebConfiguration `
-filter "system.webServer/security/authentication/*" `
-PSPath "IIS:\Sites\$siteName"

您还可以迭代站点中任何给定 Web 应用程序(甚至特定文件)的身份验证模式。以下代码检索名为“\foo”的人为 Web 应用程序的身份验证模式:

$authentications = Get-WebConfiguration `
-filter "system.webServer/security/authentication/*" `
-PSPath "IIS:\Sites\$siteName\foo"

SectionPath属性可用于检查身份验证模式,例如:

$authentications | foreach {$_.SectionPath}

哪些输出:

 /system.webServer/security/authentication/digestAuthentication
/system.webServer/security/authentication/anonymousAuthentication
/system.webServer/security/authentication/iisClientCertificateMappingAuthentication
/system.webServer/security/authentication/basicAuthentication
/system.webServer/security/authentication/clientCertificateMappingAuthentication
/system.webServer/security/authentication/windowsAuthentication

您可能认为您可以在 foreach 循环中做这样简单的事情...

 $authentications | `
foreach { $_.Enabled = $_.SectionPath.EndsWith('\windowsAuthentication') }

...但是有一个问题。这不起作用。它实际上不会因错误而失败,但也不会改变任何内容。

这是因为身份验证部分被锁定。要更改锁定部分中的设置,您需要调用 Set-WebConfigurationProperty 并包含 -Location 参数,例如

Set-WebConfigurationProperty `
-filter "/system.webServer/security/authentication/windowsAuthentication" `
-name enabled -value true -PSPath "IIS:\" -location $siteName

我想您仍然可以将对象通过管道传输到 foreach-object cmdlet,但如果您使用 foreach 循环编写脚本,则可能会更容易阅读(和维护)。

$siteName = "MySiteName"

$authentications = Get-WebConfiguration `
-filter "system.webServer/security/authentication/*" `
-PSPath "IIS:\Sites\$siteName"

foreach ($auth in $authentications)
{
$auth.SectionPath -match "/windowsAuthentication$"
$enable = ($matches.count -gt 0)

Set-WebConfigurationProperty `
-filter $auth.SectionPath `
-name enabled -value $enable -PSPath "IIS:\" -location $siteName
}

关于iis-7 - 如何在 PowerShell 中迭代应用程序 IIS7 的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820631/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com