【问题标题】:How can I change the IP address of all sites in IIS6 using powershell 1.0?如何使用 powershell 1.0 更改 IIS6 中所有站点的 IP 地址?
【发布时间】:2009-10-28 08:40: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. 不插入主机头值,而是更改现有的值。

【问题讨论】:

    标签: iis powershell iis-6 powershell-1.0 hostheaders


    【解决方案1】:

    以下 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 "========================="
            }
        }
    }
    

    【讨论】:

    • 运行此命令时,我收到以下提示... cmdlet new-object at command pipeline position 1. 为以下参数提供值:TypeName:
    • 忘了PS用反引号表示续行
    • 当某个答案对您有所帮助时,请随时为答案投票。我相信 Kev 会很感激的。 :-)
    • @Keith - 我忍不住 :)
    猜你喜欢
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2013-02-24
    • 2014-12-05
    相关资源
    最近更新 更多