Iis 7 如何在PowerShell中迭代应用IIS7的身份验证

Iis 7 如何在PowerShell中迭代应用IIS7的身份验证,iis-7,powershell,Iis 7,Powershell,我需要迭代IIS应用程序的所有身份验证模式,并禁用除一个之外的所有模式 比如: foreach($itm in [collection of authentication modes for app]){ if([certain authentication]){enabled = true}else{enabled = false}} 我熟悉Set-WebConfiguration属性。您可以通过调用Get-WebConfiguration: $siteName = "MySiteName"

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

比如:

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

我熟悉Set-WebConfiguration属性。

您可以通过调用Get-WebConfiguration:

$siteName = "MySiteName"

$authentications = Get-WebConfiguration `
                   -filter "system.webServer/security/authentication/*" `
                   -PSPath "IIS:\Sites\$siteName"
您还可以迭代站点中任何给定web应用程序(甚至特定文件)的身份验证模式。以下内容检索名为“\foo”的人工web应用程序的身份验证模式:

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-WebConfiguration属性并包含-Location参数,例如

Set-WebConfigurationProperty `
-filter "/system.webServer/security/authentication/windowsAuthentication" `
-name enabled -value true -PSPath "IIS:\" -location $siteName
我想您仍然可以通过管道将对象传输到foreach对象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
}
$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
}