簡體   English   中英

如何使用 Powershell 在 IIS 中設置“ConnectAs”用戶

[英]How to set "ConnectAs" user in IIS using Powershell

我在為 IIS 中嵌套的 web 應用編寫“ConnectAs”域用戶帳戶腳本時遇到問題。我的目標環境是 MS Server 2012 R2 和 IIS 8,但是,我在 Windows 7 和 IIS 7.

例如,假設我有“默認 Web 站點”。 在那之下我有“MainApplication”。 在此之下,我還有另一個 web 申請“子申請”。

我嘗試了在其他網站上找到的與以下類似的建議並取得了部分成功:前兩個陳述有效。

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" }

有沒有一種好的方法可以將所有站點、應用程序等腳本設置為在域用戶帳戶下運行?

我最終使用的解決方案是利用xpath來獲取每個站點,應用程序和虛擬目錄的完整路徑。 我發現每種類型都需要獨立迭代。 以下是我最終創建的解決問題的功能。

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
}

試試這個。 用你的名字改變父母和子女的名字。

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

試試下面的腳本

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM