2010-02-10 42 views
0

給定有效的$服務器,以下代碼將把所有ServerBindings從其現有子網移動到1.1.1上的一個漂亮,閃亮的新服務器。使用WMI和Powershell將新ServerBinding添加到IIS 6站點

$objWMI = [WmiSearcher] "Select * From IIsWebServerSetting WHERE Name = 'w3svc/10'" 
$objWMI.Scope.Path = "\\" + $server + "\root\microsoftiisv2"  
$objWMI.Scope.Options.Authentication = 6 
$sites = $objWMI.Get() 
foreach ($site in $sites) 
{ 
    $bindings = $site.ServerBindings 
    foreach ($binding in $bindings) 
    { 
     $binding.IP = $binding.IP -ireplace "^\d{1,3}\.\d{1,3}\.\d{1,3}","1.1.1" 
    } 
    $site.ServerBindings = $bindings 
    $site.Put() 
} 

我喜歡它。它效果很好。

當我嘗試將新的ServerBinding添加到列表中時,就會出現問題。它完全吸了我。

我可以克隆現有的與結合:

$newBinding = $existingBinding.Clone() 
    $newBinding.Hostname = "test." + $newBinding.Hostname 
    $bindings += $newBinding 

的綁定將有1個新的元素,新元素將是相同類型作爲其兄弟的,但是當我試圖更新我的$網站.ServerBindings,我敬酒:

Exception setting "ServerBindings": "Unable to cast object of type'System.Management.Automation.PSObject' to type 'Sys tem.Management.ManagementBaseObject'." 

將新元素添加到原始數組更改數組從ManagementBaseObject到PSObject?

沒有別的我嘗試似乎工作,要麼。我無法將新元素添加到$ site.ServerBindings值,因爲它是隻讀的。

我很感激任何幫助。

回答

1

嘗試添加新的服務器結合例如爲:

$bindings += $newBinding.psobject.baseobject 

這似乎是在PowerShell 2.0中,我希望很快得到修正了之前展開的PSObject。

+0

謝謝你,我的前額謝謝你! 我對這一切都很陌生,但顯然baseobject比包裝對象更通用,因此乾淨利落?無論如何,我真的很感謝你的回答。 – codepoke 2010-02-11 15:39:51

+1

基礎對象是「實際」對象。爲了使PowerShell提供動態語言功能,例如將屬性和方法添加到其他人的.NET對象,它將這些對象封裝在一個特殊的PSObject中。通常,它會考慮確保其他實體「看到」底層的「基礎」對象,但在PowerShell 2.0中,這在很多情況下都會受到阻礙。 – 2010-02-11 17:00:11

相關問題