【问题标题】:Pragmatically set IIS Setting in powershell在powershell中务实地设置IIS设置
【发布时间】:2013-12-18 16:31:08
【问题描述】:

我们有一个项目来使用 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 配置(machine.config?)中获取它以及如何修改该值以确保它设置为 true 并设置用户名和密码?任何帮助将不胜感激。

【问题讨论】:

标签: iis powershell web-applications


【解决方案1】:

经过大量搜索找到了答案,这个 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
}

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 2014-06-24
    • 2018-04-12
    • 2018-01-08
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多