如何只获得财产价值

Del*_*son 9 powershell

我想从计算机名称中检索 IP 地址,但我只想要 IP。

$computer = 'Server1'
$computer = [System.Net.Dns]::GetHostAddresses($computer) | select IPAddressToString
Run Code Online (Sandbox Code Playgroud)

返回@{IPAddressToString=xxxx}。我如何返回“xxxx”

Gre*_*ker 9

代替

| select IPAddressToString
Run Code Online (Sandbox Code Playgroud)

使用(Powershell 2.0+)

| select -First 1 -ExpandProperty IPAddressToString
Run Code Online (Sandbox Code Playgroud)

或者,如果您想使用数组

| select -ExpandProperty IPAddressToString
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个字符串数组,因此如果您想要单独的地址,请使用类似

([System.Net.Dns]::GetHostAddresses($computer) | select -ExpandProperty IPAddressToString)[0]
Run Code Online (Sandbox Code Playgroud)


Rya*_*ies 7

使用您的示例,您将键入$Computer.IPAddressToString以返回 IP 地址数组。如果该主机名只有 1 个 IP 地址,则仅此而已。然而,一个主机名可能有很多地址,这就是为什么它是一个数组。因此,如果您只想查看数组中的第一个 IP 地址,则可以键入$Computer.IPAddressToString[0]