从Powershell中的Get WebBinding获取字符串格式的IIS IP地址

从Powershell中的Get WebBinding获取字符串格式的IIS IP地址,powershell,iis,Powershell,Iis,我试图通过ID(相对于名称)检索我正在引用的网站的IP地址。我能找到的最好的方法是在“bindingInformation”属性上使用正则表达式,直到第一个冒号,如下所示 $siteID = "22" $website = Get-Website | Where { $_.ID -eq $siteID } $iP = Get-WebBinding $website.name | Where { $_.bindingInformation -match "/[^:]*/" } 但是,它似乎没有填

我试图通过ID(相对于名称)检索我正在引用的网站的IP地址。我能找到的最好的方法是在“bindingInformation”属性上使用正则表达式,直到第一个冒号,如下所示

$siteID = "22"
$website = Get-Website | Where { $_.ID -eq $siteID }
$iP = Get-WebBinding $website.name | Where { $_.bindingInformation -match "/[^:]*/" }
但是,它似乎没有填充$iP变量

当我一步一步走过时,我发现:

PS IIS:\sites> Get-WebBinding $website.name

protocol  bindingInformation                                                                        
-------- ------------------                                                                        
http      10.206.138.131:80:                                                                        
http      10.206.138.131:80:dev1.RESERVED22                                                         
http      10.206.138.131:80:dev1.www.RESERVED22 
我想我不确定的是如何转换$\绑定信息
转换为字符串格式变量?这对Powershell来说是个新概念,如果这看起来很简单,那么很抱歉。在本例中,我需要$IP变量为“10.206.138.131”。。。感谢您的帮助。

您可以使用
选择对象-ExpandProperty bindingInformation
仅获取
bindingInformation
属性值:

PS C:\> Get-WebBinding "sitename" |Select-Object -ExpandProperty bindingInformation
10.206.138.131:80:
10.206.138.131:80:dev1.RESERVED22
10.206.138.131:80:dev1.www.RESERVED22
现在,由于每个绑定字符串的形式如下:

[IP]:[Port]:[Hostname]
我们可以使用
-split
操作符将其拆分为3,只需抓取第一个:

PS C:\> $Bindings = Get-WebBinding "sitename" |Select-Object -ExpandProperty bindingInformation
PS C:\> $Bindings | ForEach-Object { @($_ -split ':')[0] }
10.206.138.131
10.206.138.131
10.206.138.131
最后,您可以使用
排序对象-唯一
删除所有重复项:

PS C:\> $Bindings = Get-WebBinding "sitename" |Select-Object -ExpandProperty bindingInformation
PS C:\> $IPs = $Bindings | ForEach-Object { @($_ -split ':')[0] }
PS C:\> $IPs = @($IPs |Sort-Object -Unique)
$IPs
变量现在是一个数组,其中包含用于绑定的所有不同IP地址,在您的示例中仅包含一个:

PS C:\> $IPs
10.206.138.131

完美的非常感谢你。我不知道-split操作符和-unique操作符。要学的东西太多了!再次感谢你,马蒂亚斯——你救了我一天……)有一个问题,[0]在:{(${-split':')[0]}中的意思是什么?很高兴能够提供帮助-只是说清楚,
-split
是一个操作符,(比如
+
=
-as
等),而
Unique
排序对象的一个参数,明白了!再次感谢你!这是完美的工作。:)
@()
生成一个数组,
[$x]
意味着我们希望数组项位于索引
$x
,因此例如
@(2,3,4)[0]
生成
2