2014-12-30 65 views
0

我正在嘗試編寫一個腳本,該腳本將循環遍歷所有本地IIS網站,並在我強制更新域密碼時更新其物理路徑憑據。通過powershell更新IIS 6 WebSite憑證

下面的作品...你運行它第一次......

function Set-Site-Credentials(
    $SiteElement, 
    $Credentials 
){ 
    $SiteElement.virtualDirectoryDefaults.userName = "$($Credentials.Domain)\$($Credentials.UserName)" 
    $SiteElement.virtualDirectoryDefaults.password = $Credentials.Password 
    $SiteElement | Set-Item -Force 
} 

運行在此之後,我注意到以下性能也得到設定

$SiteElement.userName #Same as was set earlier on .virtualDirectoryDefaults 
$SiteElement.password #Same as was set earlier on .virtualDirectoryDefaults 

隨後,我隨時嘗試使用上面的代碼更新憑據,這兩個屬性保持不變,並且這些更改不會在IIS中產生影響。

所以結果是:

$SiteElement.userName #Unchanged 
$SiteElement.password #Unchanged 
$SiteElement.virtualDirectoryDefaults.userName #New value 
$SiteElement.virtualDirectoryDefaults.password #New value 

和IIS網站仍顯示在UI老用戶名和憑據失敗。

所以很自然我想在我的更新功能設置這些額外的2個屬性:

function Set-Site-Credentials(
    $SiteElement, 
    $Credentials 
){ 
    $SiteElement.userName = "$($Credentials.Domain)\$($Credentials.UserName)" 
    $SiteElement.password = $Credentials.Password 
    $SiteElement.virtualDirectoryDefaults.userName = "$($Credentials.Domain)\$($Credentials.UserName)" 
    $SiteElement.virtualDirectoryDefaults.password = $Credentials.Password 
    $SiteElement | Set-Item -Force 
} 

的代碼拋出任何錯誤或警告,但最終的結果是一樣的,那2點額外的屬性保持不變。

我用下面的代碼獲取「$ SiteElement」

$sites = Get-ChildItem IIS:\Sites 
$sites | Foreach-Object { Set-Site-Credentials -SiteElement $_ -Credentials $newCredentials } 

此外,在我重新啓動腳本的末尾IIS使用這個命令:

Restart-Service W3SVC 

回答

1

哎,終於找到了命令的作品。總而言之,我已經嘗試了來自interwebz上不同例子的4種不同的變體,所有這些變體都只在第一次工作。但這個命令更新正確的後續變化:

function Set-Site-Credentials(
    $SiteElement, 
    $Credentials 
){ 
    Set-WebConfiguration -Filter "$($SiteElement.ItemXPath)/application[@path='/']/virtualDirectory[@path='/']" -Value @{userName="$($Credentials.Domain)\$($Credentials.UserName)"; password="$($Credentials.Password)"}  
} 

完整的腳本

param (
    [switch]$All, 
    [switch]$AllPools, 
    [switch]$AllSites, 
    [string]$AppPool, 
    [string]$Site 
) 

Import-Module WebAdministration 

function Set-AppPool-Credentials(
    $AppPoolElement, 
    $Credentials 
){ 
    Set-ItemProperty $AppPoolElement.PSPath -name processModel -value @{userName="$($Credentials.Domain)\$($Credentials.UserName)";password="$($Credentials.Password)";identitytype=3} 
} 

function Set-Site-Credentials(
    $SiteElement, 
    $Credentials 
){ 
    Set-WebConfiguration -Filter "$($SiteElement.ItemXPath)/application[@path='/']/virtualDirectory[@path='/']" -Value @{userName="$($Credentials.Domain)\$($Credentials.UserName)"; password="$($Credentials.Password)"}  
} 

$newCredentials = (Get-Credential).GetNetworkCredential() 

$appPools = Get-ChildItem IIS:\AppPools 
$sites = Get-ChildItem IIS:\Sites 


if($All -or $AllPools){ 
    $appPools | Foreach-Object { Set-AppPool-Credentials -AppPoolElement $_ -Credentials $newCredentials } 
} 
elseif($AppPool){ 
    $poolElement = ($appPools | Where-Object { $_.name -eq $AppPool }) 
    Set-AppPool-Credentials -AppPoolElement $poolElement -Credentials $newCredentials 
} 

if($All -or $AllSites){ 
    $sites | Foreach-Object { Set-Site-Credentials -SiteElement $_ -Credentials $newCredentials } 
} 
elseif($Site){ 
    $siteElement = ($sites | Where-Object { $_.name -eq $Site }) 
    Set-Site-Credentials -SiteElement $siteElement -Credentials $newCredentials 
} 

Restart-Service W3SVC