我需要迭代IIS应用程序的所有身份验证模式并禁用除一个以外的所有模式.
就像是:
foreach($itm in [collection of authentication modes for app]){
if([certain authentication]){enabled = true}else{enabled = false}}
Run Code Online (Sandbox Code Playgroud)
我熟悉Set-WebConfigurationProperty.
小智 13
您可以通过调用Get-WebConfiguration迭代给定站点的根Web应用程序的所有本机(以及任何已安装的第三方)身份验证模式:
$siteName = "MySiteName"
$authentications = Get-WebConfiguration `
-filter "system.webServer/security/authentication/*" `
-PSPath "IIS:\Sites\$siteName"
Run Code Online (Sandbox Code Playgroud)
您还可以为站点中的任何给定Web应用程序(甚至特定文件)迭代身份验证模式.以下检索名为"\ foo"的人为Web应用程序的身份验证模式:
$authentications = Get-WebConfiguration `
-filter "system.webServer/security/authentication/*" `
-PSPath "IIS:\Sites\$siteName\foo"
Run Code Online (Sandbox Code Playgroud)
SectionPath属性可用于检查身份验证模式,例如:
$authentications | foreach {$_.SectionPath}
Run Code Online (Sandbox Code Playgroud)
哪个输出:
/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
Run Code Online (Sandbox Code Playgroud)
你可能会认为你可以在foreach循环中做一些简单的事情......
$authentications | `
foreach { $_.Enabled = $_.SectionPath.EndsWith('\windowsAuthentication') }
Run Code Online (Sandbox Code Playgroud)
......但是有一个问题.它不起作用.它实际上不会因错误而失败,但它也不会改变任何东西.
那是因为身份验证部分被锁定了.要更改锁定部分中的设置,您需要调用Set-WebConfigurationProperty并包含-Location参数,例如,
Set-WebConfigurationProperty `
-filter "/system.webServer/security/authentication/windowsAuthentication" `
-name enabled -value true -PSPath "IIS:\" -location $siteName
Run Code Online (Sandbox Code Playgroud)
我想你仍然可以将对象传递给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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4973 次 |
最近记录: |