[英]Pragmatically set IIS Setting in powershell
我們有一個項目可以使用powershell設置站點的配置。 我需要知道如何將匿名訪問設置為true並將憑據設置為域用戶
我找到了一個帶有腳本示例的博客,以從應用程序的web.config獲取當前狀態。Blog單擊此處
PS C:\ > $iis = new-object Microsoft.Web.Administration.ServerManager
PS C:\ > $iis.Sites | foreach {
$_.Applications | where { $_.ApplicationPoolName -eq 'DefaultAppPool' } |
select-object Path,@{Name="AnonymousEnabled"; Expression = {
$_.GetWebConfiguration().GetSection("system.webServer/security/authentication/anonymousAuthentication").GetAttributeValue("enabled")
}}
}
但是,如果未通過web.config設置它,如何從mroot config(machine.config?)獲取它,以及如何修改該值以確保將其設置為true並設置用戶名和密碼? 任何幫助都感激不盡。
經過大量搜索后,找到了答案,這個powershell功能可以滿足我的要求。 我最終使用了appcmd。
function SetAnonymousAccess
{
Param
(
[String]$RelativePath, #The Applications Relative Path
[String]$SiteName, #The Site the app is attached to
[String]$EnableAnonymousAccess, #True or False to set AnonymousAccess
[String]$UserName, #Username of anonymous Access
[String]$Password #Password of anonymous Access
)
if($RelativePath -notlike "[/]*"){$RelativePath = "/" + $RelativePath}
$ConfAppName = $SiteName + $RelativePath
$UserCredentials =""
$msg = ""
If($EnableAnonymousAccess -And $UserName -And $Password){
$UserCredentials = "/userName:$UserName /password:$Password"
$msg = "Applied AnonymousAccess=$EnableAnonymousAccess for user:$UserName"
}else{
$msg = "Applied AnonymousAccess=$EnableAnonymousAccess"
}
& $Env:WinDir\system32\inetsrv\appcmd.exe set config $ConfAppName /section:anonymousAuthentication $UserCredentials /enabled:$EnableAnonymousAccess /commit:apphost
Write-Host $msg -foregroundColor DarkGreen
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.