2009-10-28 3 views
4

IIS 6이 설치된 Windows Server 2003에서 Powershell 1.0 사용powershell 1.0을 사용하여 IIS6의 모든 사이트의 IP 주소를 변경하려면 어떻게해야합니까?

약 200 개의 사이트가 있습니다 (웹 사이트의 "웹 사이트"탭에있는 웹 사이트 속성에 나와있는 것처럼) .

$site = [adsi]"IIS://localhost/w3svc/$siteid" 
$site.ServerBindings.Insert($site.ServerBindings.Count, ":80:$hostheader") 
$site.SetInfo() 
나는 이런 식으로 뭔가를 할 수있는 방법

:하지만

  1. 소호 식별 "섹션"IP 주소 "필드

    는이 코드를 발견 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

+0

PS가 backtick을 사용하여 회선 계속을 나타냄을 잊어 버렸습니다 – Kev

+0

awesome that worked! – User

관련 문제