首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Powershell在IIS中设置"ConnectAs“用户

如何使用Powershell在IIS中设置"ConnectAs“用户
EN

Stack Overflow用户
提问于 2014-08-09 05:02:15
回答 3查看 5.4K关注 0票数 6

我在为IIS中嵌套的web应用编写"ConnectAs“域用户帐户时遇到了困难。我的目标环境是带有IIS8的MS Server 2012 R2,但是,我在IIS7.5的Windows 7上看到了同样的问题。

例如,假设我有“默认网站”。下面是"MainApplication“。在这种情况下,我有另一个"SubApplication“的web应用程序。

我在其他类似于下面的网站上尝试了一些建议,取得了部分成功:前两项声明都有效。

代码语言:javascript
复制
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"

对于接下来的两个语句,我似乎无法理解正确的语法:

代码语言:javascript
复制
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应用程序都能在同一个域用户帐户下快速运行:

代码语言:javascript
复制
Get-WebApplication | ForEach-Object { $_ | Set-ItemProperty -Name "username" -Value "domain\user" }
Get-WebApplication | ForEach-Object { $_ | Set-ItemProperty -Name "password" -Value "passwordValue" } 

或者类似的东西:

代码语言:javascript
复制
Get-WebApplication | ForEach-Object { $_.ChildElements | Select-Object -First 1 | Get-Member -Name "Attributes" | Set-Member -Name "userName" -Value "domain\username" }

有一个好的方式来脚本设置所有的网站,应用程序等在一个域用户帐户下运行吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-05 20:57:49

我最后使用的解决方案是使用xpath来获取每个站点、app &虚拟目录的完整路径。我发现每个类型都需要独立地迭代。下面是我为解决这个问题而创建的函数。

代码语言:javascript
复制
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
}
票数 8
EN

Stack Overflow用户

发布于 2014-08-09 06:13:20

尝尝这个。用你的名字改变父母和孩子的名字。

代码语言:javascript
复制
Set-WebConfiguration "/system.applicationHost/sites/site[@name='Default Web Site']/application[@path='/parent/child']/virtualdirectory[@path='/']" -Value @{userName='andy';password='password'}
票数 0
EN

Stack Overflow用户

发布于 2022-03-11 19:58:41

试试下面的脚本

代码语言:javascript
复制
$deepestPath        =   "MACHINE/WEBROOT/APPHOST"
$sectionPath        =   "system.applicationHost/sites/site/application/virtualDirectory"
$appPhysicalPath    =   "D:\Inetpub\wwwroot\yourApp"
$userName           =   "domain\serviceaccount"
$password           =   "#######"
$fullPath=$sectionPath+"[@physicalPath='$appPhysicalPath']"
Set-WebConfigurationProperty -PSPath $deepestPath -Filter $fullPath -Name "userName" -Value $userName
Set-WebConfigurationProperty -PSPath $deepestPath -Filter $fullPath -Name "password" -Value $password
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25215354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档