4

IIS 6 を搭載した Windows Server 2003 で Powershell 1.0 を使用します。

IP アドレスを変更したいサイトが約 200 あります ([Web サイトの識別] セクションの [IP アドレス] フィールドの [Web サイト] タブにある Web サイトのプロパティに記載されています。

私はこのコードを見つけました:

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

どうすればこのようなことができますか:

  1. IIS のすべてのサイトをループする
  2. ホスト ヘッダー値を挿入するのではなく、既存の値を変更します。
4

1 に答える 1

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 "========================="
        }
    }
}
于 2009-10-28T14:06:55.410 に答える