2009-10-28 47 views
4

在帶有IIS 6的Windows Server 2003下使用Powershell 1.0。如何使用PowerShell 1.0更改IIS6中所有站點的IP地址?

我有大約200個站點,我想更改其IP地址(如「網站」選項卡上的網站屬性中所列) 。識別」部分 「IP地址」 字段

我發現這個代碼:

$site = [adsi]"IIS://localhost/w3svc/$siteid" 
$site.ServerBindings.Insert($site.ServerBindings.Count, ":80:$hostheader") 
$site.SetInfo() 

我怎樣才能做這樣的事情,但是:

  1. 羅運行IIS中的所有站點
  2. 不插入主機標頭值,但更改現有標頭值。

回答

10

下面的PowerShell腳本應該有所幫助:

$oldIp = "172.16.3.214" 
$newIp = "172.16.3.215" 

# Get all objects at IIS://Localhost/W3SVC 
$iisObjects = new-object ` 
    System.DirectoryServices.DirectoryEntry("IIS://Localhost/W3SVC") 

foreach($site in $iisObjects.psbase.Children) 
{ 
    # Is object a website? 
    if($site.psbase.SchemaClassName -eq "IIsWebServer") 
    { 
     $siteID = $site.psbase.Name 

     # Grab bindings and cast to array 
     $bindings = [array]$site.psbase.Properties["ServerBindings"].Value 

     $hasChanged = $false 
     $c = 0 

     foreach($binding in $bindings) 
     { 
      # Only change if IP address is one we're interested in 
      if($binding.IndexOf($oldIp) -gt -1) 
      { 
       $newBinding = $binding.Replace($oldIp, $newIp) 
       Write-Output "$siteID: $binding -> $newBinding" 

       $bindings[$c] = $newBinding 
       $hasChanged = $true 
      } 
      $c++ 
     } 

     if($hasChanged) 
     { 
      # Only update if something changed 
      $site.psbase.Properties["ServerBindings"].Value = $bindings 

      # Comment out this line to simulate updates. 
      $site.psbase.CommitChanges() 

      Write-Output "Committed change for $siteID" 
      Write-Output "=========================" 
     } 
    } 
} 
+0

運行此我得到以下提示......在命令管道位置cmdlet的新對象1.供應值以下參數:類型名: – User 2009-10-28 17:08:17

+0

忘記PS使用反色指示線延續 – Kev 2009-10-28 18:17:15

+0

真棒工作! – User 2009-10-28 18:56:27