在Powershell中为IIS应用启用身份验证

在Powershell中为IIS应用启用身份验证,powershell,iis,web-deployment,Powershell,Iis,Web Deployment,我知道您是如何通过以下命令设置IIS网站的: Set-WebConfigurationProperty -filter "/system.webServer/security/authentication/windowsAuthentication" -name enabled -value true -PSPath "IIS:\" -location $siteName 但我想为该网站内的应用程序设置它。例如,我有一个名为“MySite”的IIS网站,里面有两个应用程序。我想为其中一个启用W

我知道您是如何通过以下命令设置IIS网站的:

Set-WebConfigurationProperty -filter "/system.webServer/security/authentication/windowsAuthentication" -name enabled -value true -PSPath "IIS:\" -location $siteName

但我想为该网站内的应用程序设置它。例如,我有一个名为“MySite”的IIS网站,里面有两个应用程序。我想为其中一个启用Windows身份验证,而不是为另一个启用Windows身份验证。因此,在站点级别启用将同时启用这两个参数,而这正是我不想要的。

您不需要单独的
-PSPath
-Location
参数。您可以这样组合它们:

-PSPath "IIS:\Sites\$SiteName\$AppName"
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/windowsAuthentication" -Name Enabled -Value True -PSPath "IIS:\Sites\$SiteName\$AppName"
因此,实际的命令如下所示:

-PSPath "IIS:\Sites\$SiteName\$AppName"
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/windowsAuthentication" -Name Enabled -Value True -PSPath "IIS:\Sites\$SiteName\$AppName"
请注意,您可能会遇到以下错误:

Set-WebConfiguration属性:此配置节不能在此路径上使用。当节在父级锁定时会发生这种情况。锁定在默认情况下(overrideModeDefault=“Deny”),或者由带有overrideMode=“Deny”或旧版allowOverride=“false”的位置标记显式设置

Tomfanning at ServerFault提供了解决方案。我在这里重复了他的步骤:

  • 打开IIS管理器
  • 单击左侧树中的服务器名称
  • 右窗格,管理部分,双击配置编辑器
  • 在顶部,选择system.webServer/security/authentication/anonymousAuthentication部分
  • 在右侧窗格中,单击“解锁”部分
  • 在顶部,选择system.webServer/security/authentication/windowsAuthentication部分
  • 在右侧窗格中,单击“解锁”部分

  • 我遇到了处理锁定部分的问题,而公认的答案是打开一个GUI来解决这个问题,我正试图首先用PowerShell来避免这个问题

    简短回答 启用Windows身份验证并禁用匿名身份验证

    $iisSiteName = "Default Web Site"
    $iisAppName = "MyApp"
    
    Write-Host Disable anonymous authentication
    Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/anonymousAuthentication' -Name 'enabled' -Value 'false' -PSPath 'IIS:\' -Location "$iisSiteName/$iisAppName"
    
    Write-Host Enable windows authentication
    Set-WebConfigurationProperty -Filter '/system.webServer/security/authentication/windowsAuthentication' -Name 'enabled' -Value 'true' -PSPath 'IIS:\' -Location "$iisSiteName/$iisAppName"
    
    处理锁定部分 如报告所述:

    身份验证部分通常被锁定,即无法写入 到web.config文件,但必须写入中心 而不是applicationhost.config文件

    我们必须使用
    -PSPath
    -Location
    参数

    Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath IIS:\ -location DemoSite/DemoApp
    

    谢谢@rickglos,“处理锁定部分”解决了这个问题。其他人注意:分离PSPath和位置是避免锁定问题所必需的!分离-PSPath和-Location可以避免锁定问题!见下文@rickglos。