简体   繁体   English

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

[英]Get an IIS IP address in string format from Get-WebBinding in Powershell

I am trying to retrieve the IP address of a website that I am referencing via the ID (vs. the name). 我试图通过ID(与名称)检索我引用的网站的IP地址。 The best way I can figure out to do it is using a regex on the "bindingInformation" property going up to the first colon as below... 我能想到的最好的方法是在“bindingInformation”属性上使用正则表达式,直到第一个冒号,如下所示...

$siteID = "22"
$website = Get-Website | Where { $_.ID -eq $siteID }
$iP = Get-WebBinding $website.name | Where { $_.bindingInformation -match "/[^:]*/" }

However it doesn't seem to be populating the $iP variable? 但它似乎没有填充$ iP变量?

As I step through I get this: 当我单步执行时,我得到了这个:

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 

I guess what I'm not sure about is how to convert the $_.bindingInformation into a string format variable? 我想我不确定的是如何将$ _。bindingInformation转换为字符串格式变量? Pretty new to Powershell so sorry if this seems simple. 对Powershell来说很新鲜,如果这看起来很简单,那就很抱歉。 I need the $IP variable to be "10.206.138.131" in this example... Thank you for any help. 在这个例子中我需要$ IP变量为“10.206.138.131”...感谢您的帮助。

You can use Select-Object -ExpandProperty bindingInformation to grab just the bindingInformation property value: 您可以使用Select-Object -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

Now, since each binding string is in the form: 现在,因为每个绑定字符串的形式如下:

[IP]:[Port]:[Hostname]

We can use the -split operator to split it into 3 and just grab the first one: 我们可以使用-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

Finally, you can use Sort-Object -Unique to remove all duplicates: 最后,您可以使用Sort-Object -Unique删除所有重复项:

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)

The $IPs variable is now an array containing all the distinct IP addresses used for bindings, in your case just a single one: $IPs变量现在是一个包含用于绑定的所有不同IP地址的数组,在您的情况下只是一个:

PS C:\> $IPs
10.206.138.131

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM