务实设置PowerShell中的IIS设置

时间:2013-12-18 16:31:08

标签: iis powershell web-applications

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

我找到了一个带有脚本示例的博客,以从应用程序的web.config获取当前状态 Blog click here

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并设置用户名和密码?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

经过多次搜索找到答案这个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
}