iis PowerShell - Web管理-仅获取443绑定-遇到问题

xmakbtuz  于 2023-05-07  发布在  Shell
关注(0)|答案(2)|浏览(163)

我有这个代码,让我的网站绑定,这是伟大的。但它让我所有的绑定。有没有办法过滤它,所以我只是得到443绑定?

Import-Module Webadministration
Foreach ($Site in get-website) { Foreach ($Bind in $Site.bindings.collection) {[pscustomobject]@{Bindings=$Bind.bindinginformation.Split(":")[-1]}}}

这给了我一个类似的列表;

  • site1.awesome.com
  • site1.awesome.com
  • site2.awesome.com
  • site2.awesome.com

近乎完美。我只是想在列表中只有443个绑定。

eit6fx6z

eit6fx6z1#

如果您只需要绑定到443的站点,则只需添加该条件。

Foreach ($Site in get-website) { Foreach ($Bind in $Site.bindings.collection | where bindinginformation -like '*443*') {[pscustomobject]@{Bindings=$Bind.bindinginformation.Split(":")[-1]}}}

我还建议您将命令拆分,以便更易于阅读和维护。

$sitelist = Get-Website

Foreach ($site in $sitelist) {
    $bindinglist = $site.bindings.collection | where bindinginformation -like '*443*'

    Foreach ($bind in $bindinglist) {
        [pscustomobject]@{
            Bindings = $bind.bindinginformation.Split(":")[-1]
        }
    }
}
esyap4oy

esyap4oy2#

我将从以下代码片段开始:

#Requires -RunAsAdministrator
Import-Module Webadministration
Foreach ($Site in get-website) {
    Foreach ($Bind in $Site.bindings.collection) {
        [pscustomobject]@{
            SiteName = $Site.Name
            Bindings = $Bind.bindinginformation
        }
    }
}

然后,缩小输出以获得443绑定,如下所示:

Foreach ($Site in get-website) {
    Foreach ($Bind in $Site.bindings.collection) {
        if ($Bind.bindinginformation -match ':443') {
            [pscustomobject]@{
                SiteName = $Site.Name
                Bindings = $Bind.bindinginformation
            }
        }
    }
}

输出 * 可能 * 如下所示:

SiteName          Bindings
--------          --------
site1.awesome.com *:443:
site2.awesome.com *:443:

相关问题