【问题标题】:Swap IIS Bindings Between Two Sites在两个站点之间交换 IIS 绑定
【发布时间】:2016-01-21 22:30:36
【问题描述】:

我正在尝试在 2 个站点之间交换绑定,即 Powershell 中的 IIS。下面的脚本正在运行,但它似乎有点太复杂了:

    $site1Name = ''
    $site2Name = ''
    
    $site1 = Get-Website | Where-Object {$_.Name -eq $site1Name}
    $site2 = Get-Website | Where-Object {$_.Name -eq $site2Name}

    $site1Bindings = $site1 | Get-WebBinding
    $site2Bindings = $site2 | Get-WebBinding
    
    $site1 | Get-WebBinding | Remove-WebBinding
    $site2 | Get-WebBinding | Remove-WebBinding
    
    function Copy-Bindings
    {
        param($siteA_Bindings, [string]$siteB_Name)
         
        foreach ($binding in $siteA_Bindings)
        {
            $bindingInformation = $binding['bindingInformation'].Split(':')
            $ip = $bindingInformation[0]
            $port = $bindingInformation[1]
            
            if ($bindingInformation.Length -gt 2)
            {   
                $hostRecord = $bindingInformation[2]
            }
            else
            {   
                $hostRecord = ''
            }
            
            $protocol = $binding['protocol']
            New-WebBinding -Name $siteB_Name -Port $port -Protocol $protocol -IPAddress $ip -HostHeader $hostRecord
        }
    }
    
    Copy-Bindings $site1Bindings $site2Name
    Copy-Bindings $site2Bindings $site1Name

我想要的是更像这样的东西(在伪代码中):

    $site1Name = ''
    $site2Name = ''
    
    $site1 = Get-Website | Where-Object {$_.Name -eq $site1Name}
    $site2 = Get-Website | Where-Object {$_.Name -eq $site2Name}

    $site1Bindings = $site1 | Get-WebBinding
    $site2Bindings = $site2 | Get-WebBinding
    
    $site1 | Get-WebBinding | Remove-WebBinding
    $site2 | Get-WebBinding | Remove-WebBinding
    
    $site1Bindings | New-WebBinding -Name $site2Name
    $site2Bindings | New-WebBinding -Name $site1Name

有什么方法可以简化吗?

谢谢,

【问题讨论】:

    标签: powershell iis


    【解决方案1】:

    简短的回答,不,它不能被简化到所需的伪代码所代表的程度。当前的功能对于 IPv4 站点绑定来说足够好,但需要对 IPv6 绑定进行更多调整(在 [ 和 ] 上拆分除了:)。

    help file for New-WebBinding 表明,虽然需要的参数确实采用管道输入,但它是 ByPropertyName(如果属性名称匹配则有效)而不是 ByValue(如果对象类型匹配则有效)。

    仔细看看我们从$site1Bindings 变量的示例结果中得到的结果说明了原因。首先,我们来看看对象类型:

    PS C:\windows\system32> $site2Bindings | GM
    
       TypeName:
    Microsoft.IIs.PowerShell.Framework.ConfigurationElement#bindings#binding
    

    不幸的是,New-WebBinding 不接受该对象类型作为任何参数的输入。这排除了直接使用它来流水线到New-WebBinding。但是,$site2Bindings 中的属性名称呢?下面是一个例子:

    PS C:\windows\system32> $site2Bindings | FL
    
    protocol           : http
    bindingInformation : [2002:4000:200:9:900:e45e:e501:aa37]:80:
    sslFlags           : 0
    

    嗯,protocol 匹配参数的名称,因此可以通过管道输入使用,但这是唯一的。因此,您必须将结果信息切分并将其分配给变量,就像您在函数中所做的那样。可能有更简洁的方法来分割结果,但在使用 New-WebBinding 时无法避免。

    【讨论】:

    • 您能否建议一种更简洁的方法来分割结果(或不需要的 New-WebBinding 的替代方法)?
    • 我查看了其他一些*IIS* config cmdlet,发现那里也有同样丑陋的绑定信息格式。我会四处看看是否有其他选择。
    猜你喜欢
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2013-05-26
    相关资源
    最近更新 更多