在Powershell中务实地设置IIS设置



我们有一个项目,使用 powershell 设置站点的配置。我需要知道如何将匿名访问设置为 true 并将凭据设置为域用户

我找到了一个带有脚本示例的博客,可以从应用程序的web.config中获取当前状态博客点击这里

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配置(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:WinDirsystem32inetsrvappcmd.exe set config $ConfAppName /section:anonymousAuthentication $UserCredentials /enabled:$EnableAnonymousAccess  /commit:apphost
    Write-Host $msg -foregroundColor DarkGreen
}

最新更新