PowerShell 1.0을 사용하여 IIS6의 모든 사이트의 IP 주소를 어떻게 변경할 수 있습니까?

StackOverflow https://stackoverflow.com/questions/1635882

문제

IIS 6과 함께 Windows Server 2003에서 PowerShell 1.0 사용.

"웹 사이트 식별"섹션 "IP 주소"필드의 "웹 사이트"탭의 웹 사이트 속성에 나열된대로 IP 주소를 변경하려는 약 200 개의 사이트가 있습니다.

이 코드를 찾았습니다.

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

어떻게 이런 일을 할 수 있지만 :

  1. IIS의 모든 사이트를 루프하십시오
  2. 호스트 헤더 값을 삽입하지 말고 기존 값을 변경하십시오.
도움이 되었습니까?

해결책

다음 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 "========================="
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top