powershell - 将 Get-WebBinding 输出转换为 New-WebBinding 命令

标签 powershell iis-7.5

作为部署脚本的一部分,我想保存当前网站上的所有绑定(bind)——但我想将它们保存为可用于以后重新创建它们的格式。

到目前为止,我有这个:

    Import-module WebAdministration
    (Get-Item IIS:\Sites\$siteName).Bindings.Collection | select-object @{ Label="CMD"; Expression={"New-WebBinding -name '{0}' -Protocol {1} -IPAddress '{2}' -Port {3} -HostHeader '{4}'" -f "#siteName",$_.protocol,$_.bindingInformation.split(":")[0],$_.bindingInformation.split(":")[1],$_.bindingInformation.split(":")[2]}} | findstr "New-WebBinding" >c:\temp\bindings-$siteName.ps1

这会产生如下有用的输出:
New-WebBinding -name 'w2pclient' -Protocol http -IPAddress '172.16.7.17' -Port 80 -HostHeader ''   
New-WebBinding -name 'w2pclient' -Protocol http -IPAddress '172.16.7.16' -Port 80 -HostHeader ''   
New-WebBinding -name 'w2pclient' -Protocol https -IPAddress '172.16.7.18' -Port 443 -HostHeader '' 
New-WebBinding -name 'w2pclient' -Protocol https -IPAddress '172.16.7.36' -Port 443 -HostHeader '' 

不幸的是,正如您所见,SSL 绑定(bind)缺少两件事:
  • 证书的指纹(-ThumbprintNew-WebBinding 参数)
  • 它是哪种 SSL 绑定(bind)(-SslFlags 参数为 New-WebBinding)

  • 我开始使用 dir IIS:\\SslBindings作为替代方案,我知道它如何让我获得 Thumbprint但不是 SslFlags .

    有任何想法吗?

    [编辑] Jan Chrbolka 给了我答案。这是我的最终脚本:
    function Record-Bindings([string]$siteName)
    {
        Import-module WebAdministration
        (Get-Item IIS:\Sites\$siteName).Bindings.Collection | 
          Select-Object bindinginformation, Protocol, @{ name="IP"; expression={$_.bindinginformation.split(":")[0]}}, 
         @{name="Port"; expression={$_.bindinginformation.split(":")[1]}}, 
         @{name="HostName"; expression={$_.bindinginformation.split(":")[2]}},
         @{name="certificateHash"; expression={$_.GetAttributeValue("certificateHash")}},
         @{name="sslFlags"; expression={$_.GetAttributeValue("sslFlags")}} | 
         Where-Object {$_.Port -lt 8000} |
         ForEach-Object { "New-WebBinding -name '{0}' -Protocol {1} -IPAddress '{2}' -Port {3} -HostHeader '{4}' -Thumbprint '{5}' -SslFlags {6}" `
         -f "$siteName",$_.protocol,$_.IP, $_.Port, $_.HostName, $_.certificateHash, $_.sslFlags } > c:\temp\bindings-$siteName.ps1
    
    }
    

    最佳答案

    您需要的参数值位于 Bindings.Collection 中。

    您需要使用 获取属性值 方法来检索它们。

    像这样的东西...

    (Get-Item IIS:\Sites\TEST).Bindings.Collection[1].GetAttributeValue("certificateHash")
    (Get-Item IIS:\Sites\TEST).Bindings.Collection[1].GetAttributeValue("sslFlags")
    

    关于powershell - 将 Get-WebBinding 输出转换为 New-WebBinding 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30582170/

    相关文章:

    powershell - Arduino 通过 PowerShell 脚本与 PC 通信

    asp.net-mvc-4 - 尝试从 Visual Studio 2012 项目目录运行站点时出现 HTTP 错误 500.19

    asp.net - 使用本地系统应用程序池标识时,进程仅适用于 Web 应用程序

    c# - 检测 ASP.NET 中的内存泄漏

    scripting - 重命名文件列表并在Powershell中创建文件夹

    powershell - 我可以在Powershell中自定义 'not recognized as the name of a cmdlet'错误吗?

    svn - 为什么这个 PowerShell 脚本删除 svn :mergeinfo from the root directory?

    bash - 在powershell中使用mkdir创建多个子目录

    c# - 性能分析 asp.net,什么是 ProcessRequestNotificationHelper?

    iis-7.5 - CORS 选项响应在 IISExpress 中有效,但在 IIS7.5 中无效