简体   繁体   中英

Pragmatically set IIS Setting in powershell

We have a project to set up a site's configuration with powershell. I need to know how to Set Anonymous Access to true and set the credentials to a domain user

I found a blog with script example to get the current state from the web.config of the application 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") 
}} 
}

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? any help will be much appreciated.

After much searching Found the answer this powershell function does what i require. I ended up using 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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