简体   繁体   English

在Powershell中实用地设置IIS设置

[英]Pragmatically set IIS Setting in powershell

We have a project to set up a site's configuration with powershell. 我们有一个项目可以使用powershell设置站点的配置。 I need to know how to Set Anonymous Access to true and set the credentials to a domain user 我需要知道如何将匿名访问设置为true并将凭据设置为域用户

I found a blog with script example to get the current state from the web.config of the application Blog click here 我找到了一个带有脚本示例的博客,以从应用程序的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") 
}} 
}

But if it is not set through web.config, how do i get it from the mroot config (machine.config?) and how do I modify the value to make sure it is set to true and set the username and password? 但是,如果未通过web.config设置它,如何从mroot config(machine.config?)获取它,以及如何修改该值以确保将其设置为true并设置用户名和密码? any help will be much appreciated. 任何帮助都感激不尽。

After much searching Found the answer this powershell function does what i require. 经过大量搜索后,找到了答案,这个powershell功能可以满足我的要求。 I ended up using appcmd. 我最终使用了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM