How to authenticate for an IIS7 application in PowerShell

I need to repeat all authentication modes for the IIS application and disable all but one.

sort of:

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

I am familiar with Set-WebConfigurationProperty.

+5
source share
1 answer

You can iterate all your own (as well as any installed third-party) authentication modes for the root web application for this site by calling Get-WebConfiguration:

$siteName = "MySiteName"

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

You can also iterate over authentication modes for any given web application on a site (or even specific files). The following are authentication modes for a fictitious web application called "\ foo":

$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, , , ( ), script 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
}
+13

All Articles