簡體   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