代码之家  ›  专栏  ›  技术社区  ›  User

如何使用PowerShell 1.0更改iis6中所有站点的IP地址?

  •  4
  • User  · 技术社区  · 15 年前

    在带有IIS 6的Windows Server 2003下使用PowerShell 1.0。

    我有大约200个要更改其IP地址的站点(如“网站标识”部分“IP地址”字段中“网站”选项卡上的“网站属性”所列)。

    我发现这个代码:

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

    我怎么能这样做,但是:

    1. 循环访问IIS中的所有站点
    2. 不插入主机头值,但更改现有值。
    1 回复  |  直到 15 年前
        1
  •  10
  •   erlando    15 年前

    以下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 "========================="
            }
        }
    }
    
    推荐文章