iis - 如何使用 powershell 1.0 更改 IIS6 中所有站点的 IP 地址?

标签 iis powershell iis-6 powershell-1.0 hostheaders

在带有 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()

我怎么能做这样的事情,但是:
  • 遍历 IIS 中的所有站点
  • 不插入主机头值,而是更改现有的值。
  • 最佳答案

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

    关于iis - 如何使用 powershell 1.0 更改 IIS6 中所有站点的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1635882/

    相关文章:

    iis - 是否可以从 web.config 文件为 IIS 设置经典 ASP 设置?

    asp.net - 防止 muieblackcat BOT

    asp.net - 无法识别的属性 'controlRenderingCompatibilityVersion'

    iis - 如何配置MVC6应用程序以在IIS上工作?

    windows - 用于将每个第 n 个文件从 folder1 复制到 folder2 的批处理文件?

    regex - Powershell-用以美元符号结尾的变量替换字符串

    用于输出已安装程序的 Powershell 脚本

    sql-server - 如何: Configure IIS7 for Web Synchronization

    c# - 应用程序可以在不传递 Application_End 或 Application_Error 的情况下返回 "die"吗?

    asp.net - 带有 IIS 6.0 和 .Net 4 的 Default.aspx?