2013-12-18 52 views
0

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

我发现剧本例如博客从应用 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中获取的当前状态

但是,如果它不是通过web.config设置的,我如何从mroot配置文件(machine.config?)中获取它,并且如何修改该值以确保它设置为true并设置用户名和密码?任何帮助都感激不尽。

+0

这篇文章可能会帮助你,请按照@AUSTX_RJL评论我的答案给出的链接。 http://stackoverflow.com/questions/13058537/configuring-iis-with-powerhsell-enable-forms-authentication/13058814#13058814 – dugas

+0

@Shay Levy的回答:http://stackoverflow.com/questions/18136128/toggle-iis -7-5-authentication-anonymous-authentication-with-powershell-3-0也许可以帮助你引导你。 – dugas

回答

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 
}