2009-10-28 27 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