2011-01-27 4 views

Respuesta

13

Se puede recorrer todas nativas (así como cualquier tercero instalado) modos de autenticación de la aplicación web raíz de un sitio determinado llamando Get-WebConfiguration:

$siteName = "MySiteName" 

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

También se pueden recorrer los modos de autenticación para cualquier aplicación web dada en el sitio (o incluso archivo (s) específico (s)). El siguiente recupera los modos de autenticación para una aplicación ideada web llamado "\ foo":

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

La propiedad SectionPath puede ser utilizado para examinar el modo de autenticación, por ejemplo:

$authentications | foreach {$_.SectionPath} 

que da salida:

/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 

se podría pensar que podría hacer algo tan simple como esto en su bucle foreach ...

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

... pero hay un problema. No funciona En realidad no fallará con un error, pero tampoco cambiará nada.

Eso es porque las secciones de autenticación están bloqueadas. Para cambiar una configuración en una sección cerrada, es necesario el establecimiento de llamada WebConfigurationProperty e incluir el parámetro -ubicación, por ejemplo,

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

supongo que todavía se puede canalizar los objetos al cmdlet ForEach-objeto, sino que probablemente va para ser mucho más fácil de leer (y mantener) si se script esto usando un ciclo 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 
} 
Cuestiones relacionadas