如何使用powershell在IIS中启用自定义帐户的匿名身份验证



我可以使用下面的命令使用powershell启用匿名身份验证。

Set-WebConfigurationProperty -filter /system.webServer/security/authentication/anonymousAuthentication -PSPath IIS:\Sites\test1 -Name Enabled -Value True

但我还需要添加自定义帐户的用户名和密码(test/test123),同时启用匿名身份验证而不是默认用户。有人能提供相同的命令吗?

我不是powershell专家。但是,powershell的每个参数只接受一个参数。如果你想在一条语句中传递多个参数,这比执行三条语句要麻烦得多。

或者你可以把这三条语句写进一个脚本,你可以通过执行一次脚本来启用匿名认证和同时设置用户。

  1. 创建txt文件,重命名为xxx.ps1。输入下面的powershell命令。

    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/security/authentication/anonymousAuthentication" -name "enabled" -value "True"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/security/authentication/anonymousAuthentication" -name "userName" -value "test"
    Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter "system.webServer/security/authentication/anonymousAuthentication" -name "password" -value "test123"
    
  2. 打开powershell,输入command

,"D: change.ps1">

那么它将工作。

APPCMD.exe是用于管理IIS 7及以上版本的单个命令行工具。(源)

该工具位于:C:WindowsSystem32inetsrv

你可以在PowerShell中这样使用:

cd C:WindowsSystem32inetsrv
.appcmd.exe set config "SiteName" -section:system.webServer/security/authentication/anonymousAuthentication /enabled:"True" /commit:apphost
.appcmd.exe set config "SiteName" -section:system.webServer/security/authentication/anonymousAuthentication /userName:"IUSR" /commit:apphost
.appcmd.exe set config "SiteName" -section:system.webServer/security/authentication/anonymousAuthentication /password:"P@ssw0rd" /commit:apphost

命令细节在这里

最新更新