繁体   English   中英

如何在PowerShell中为应用程序IIS7重用身份验证

[英]How Do You Iterate Authentication for App IIS7 In PowerShell

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

就像是:

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

我熟悉Set-WebConfigurationProperty。

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

$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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM