[英]Pragmatically set IIS Setting in powershell
我们有一个项目可以使用powershell设置站点的配置。 我需要知道如何将匿名访问设置为true并将凭据设置为域用户
我找到了一个带有脚本示例的博客,以从应用程序的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")
}}
}
但是,如果未通过web.config设置它,如何从mroot config(machine.config?)获取它,以及如何修改该值以确保将其设置为true并设置用户名和密码? 任何帮助都感激不尽。
经过大量搜索后,找到了答案,这个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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.