【问题标题】:How to set "ConnectAs" user in IIS using Powershell如何使用 Powershell 在 IIS 中设置“ConnectAs”用户
【发布时间】:2014-08-09 05:02:15
【问题描述】:

我在为 IIS 中的嵌套 Web 应用程序编写“ConnectAs”域用户帐户脚本时遇到问题。我的目标环境是带有 IIS 8 的 MS Server 2012 R2,但是,我在带有 IIS 7.5 的 Windows 7 上看到了同样的问题。

例如,假设我有“默认网站”。在此之下,我有“MainApplication”。在此之下,我有另一个“子应用程序”的网络应用程序。

我尝试了在其他网站上找到的类似以下的建议,但部分成功: 前 2 个语句有效。

Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='Default Web Site']/application[@path='/MainApplication']/virtualDirectory[@path='/']" -name "username" -value "mydomain\user" 
Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='Default Web Site']/application[@path='/MainApplication']/virtualDirectory[@path='/']" -name "password" -value "mypassword"

我似乎无法为接下来的两条语句找到正确的语法:

Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='Default Web Site']/application[@path='/MainApplication/SubApplication']/virtualDirectory[@path='/']" -name "username" -value "mydomain\user" 
Set-WebConfigurationProperty "system.applicationHost/sites/site[@name='Default Web Site']/application[@path='/MainApplication/SubApplication']/virtualDirectory[@path='/']" -name "password" -value "mypassword"

在一个完美的世界里,我可以做类似下面的事情来快速让所有的 web 应用程序在同一个域用户帐户下运行:

Get-WebApplication | ForEach-Object { $_ | Set-ItemProperty -Name "username" -Value "domain\user" }
Get-WebApplication | ForEach-Object { $_ | Set-ItemProperty -Name "password" -Value "passwordValue" } 

或类似的东西:

Get-WebApplication | ForEach-Object { $_.ChildElements | Select-Object -First 1 | Get-Member -Name "Attributes" | Set-Member -Name "userName" -Value "domain\username" }

有没有一种好方法可以将所有网站、应用程序等脚本设置为在域用户帐户下运行?

【问题讨论】:

    标签: powershell iis permissions


    【解决方案1】:

    我最终使用的解决方案是利用 xpaths 来获取每个站点、应用程序和虚拟目录的完整路径。我发现每种类型都需要独立迭代。以下是我最终为解决问题而创建的功能。

    function Set-IIS-ConnectAsUser($username, $password)
    {
        $dir = Get-Location
    
        cd IIS:\Sites
    
        # update all the web sites to run under the context of the specified user
        $webSites = Get-Website
        ForEach($webSite in $webSites)
        {
            $siteName = ($webSite | Select -Property "Name").name
            $fullPath = "system.applicationHost/sites/site[@name='$siteName']/application[@path='/']/virtualDirectory[@path='/']"
            Set-WebConfigurationProperty $fullPath -Name "username" -Value $username
            Set-WebConfigurationProperty $fullPath -Name "password" -Value $password
        }
    
        # update all the web applications to run under the context of the specified user
        $apps = Get-WebApplication
        ForEach($app in $apps)
        {
            $xpath = ($app | Select -Property "ItemXPath").ItemXPath
            $fullPath = "$xpath/virtualDirectory[@path='/']" 
            $fullPath = $fullPath.Substring(1)
            Set-WebConfigurationProperty $fullPath -Name "username" -Value $username
            Set-WebConfigurationProperty $fullPath -Name "password" -Value $password
        }
    
        # update all the virtual directories to run under the context of the specified user
        $virtualDirs = Get-WebVirtualDirectory
        ForEach($vdir in $virtualDirs)
        {
            $xpath = ($vdir | Select -Property "ItemXPath").ItemXPath
            $fullPath = $xpath.Substring(1)
            Set-WebConfigurationProperty $fullPath -Name "username" -Value $username
            Set-WebConfigurationProperty $fullPath -Name "password" -Value $password
        }
    
    
        cd $dir
    }
    

    【讨论】:

    • 谢谢先生!如果我每次更改密码都有一分钱,我会把它全部给你,你现在已经是百万富翁了。 ;)
    【解决方案2】:

    试试这个。用你的名字更改父母和孩子的名字。

    Set-WebConfiguration "/system.applicationHost/sites/site[@name='Default Web Site']/application[@path='/parent/child']/virtualdirectory[@path='/']" -Value @{userName='andy';password='password'}
    

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2010-11-05
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 2015-05-12
      • 2014-06-24
      相关资源
      最近更新 更多