简体   繁体   English

PowerCLI - 通过VM IP查找虚拟机

[英]PowerCLI - Finding Virtual Machine via VM IP

I am trying to locate specific VM's based from IP addresses in PowerCLI. 我试图根据PowerCLI中的IP地址找到特定的VM。 I found this script online Grabbing VM ipaddress via PowerCLI 我发现这个脚本通过PowerCLI在线抓取VM ipaddress

The intial question explaines the issues I was having, and the answer looks to resolve such issues, however when I run such script: 最初的问题解释了我遇到的问题,答案看起来解决了这些问题,但是当我运行这样的脚本时:

Get-View -ViewType VirtualMachine | Select @{N='IP';E={[string]::Join(',',$_.Guest.net.IPAddress)}}

All I get is the following output: 我得到的是以下输出:

IP
--

And that's it... Am I missing input such as specifying a cluster or DC, does this work for anyone else? 就是这样......我错过了指定集群或DC的输入,这对其他人有用吗?

Get-View

As KERR pointed out, the code in bxm's answer is faster than the code in my alternative solution below. 正如KERR指出的那样, bxm答案中的代码比我下面替代解决方案中的代码要快。 [It was, consistently, 4 times faster for me instead of 10 times faster as KERR claims; [对于我而言,它一直是快4倍,而不是KERR声称的快10倍; but still faster.] 但还是更快。]

But note tho that for the view objects returned by Get-View , the Guest.IPAddress property consists of a single address and it may not even be an address for a NIC (it may be, eg a VPN connection). 但请注意,对于Get-View返回的视图对象, Guest.IPAddress属性由单个地址组成,甚至可能不是NIC的地址(可能是,例如VPN连接)。

Here's a one-line (tweaked) version of bxm's code: 这是bxm代码的单行(调整)版本:

Get-View -ViewType VirtualMachine | ?{ $_.Guest.IPAddress -eq "1.2.3.4" }

and here's a version that should check all of the NIC addresses: 这是一个应该检查所有NIC地址的版本:

Get-View -ViewType VirtualMachine | ?{ ($_.Guest.Net | %{ $_.IpAddress }) -contains "1.2.3.4" }

where "1.2.3.4" is the IP address for which you want to find the corresponding VM. 其中"1.2.3.4"是您要查找相应VM的IP地址。

Note that my version is slightly different than bxm's. 请注意,我的版本与bxm略有不同。 bxm's version effectively ensures that any matching VMs only have the specified IP address assigned and no others (or, rather, it would if the Guest.IPAddress property was an array). bxm的版本有效地确保任何匹配的VM 分配指定的IP地址而不分配其他 VM(或者更确切地说,如果Guest.IPAddress属性是数组)。 My version only ensures that the VM has that specified address, regardless of any other IP addresses it's assigned. 我的版本仅确保VM具有指定的地址,而不管其分配的任何其他IP地址。

Get-VM

Here's my adaptation of the code at the link provided by StackUser_py's answer : 这是我在StackUser_py的回答提供的链接中对代码的改编:

Get-VM | Where-Object -FilterScript { $_.Guest.Nics.IPAddress -contains "1.2.3.4" }

Note tho that these two solutions return different results, the first an (array of) VirtualMachine (objects), and the second a UniversalVirtualMachineImpl . 注意这两个解决方案返回不同的结果,第一个是(数组) VirtualMachine (对象),第二个是UniversalVirtualMachineImpl However, calling Get-VM and passing it the name of the VM returned by the first solution does not significantly alter the duration. 但是,调用Get-VM并向其传递第一个解决方案返回的VM的名称不会显着改变持续时间。

I got the command working with a small tweak to the objects used, like so: 我得到了命令,对所使用的对象进行了一些小调整,如下所示:

$list = Get-View -ViewType VirtualMachine | Select name,@{N='IP';E={[string]::Join(',',$_.Guest.ipaddress)}}
$list | ?{ $_.ip -eq "1.2.3.4" }

Although I'm not sure why the above still doesn't work, i found the following that may help people. 虽然我不确定为什么以上仍然不起作用,但我发现以下可能有助于人们。 Very useful for Large VM enviroments. 对大型虚拟机环境非常有用。 (This is what I was trying to script from the above initially). (这是我最初尝试从上面编写的脚本)。

Using PowerCLI to Find a Specific Guest IP 使用PowerCLI查找特定的访客IP

或者,你可以像这样去做。

Get-VM | Where-Object {$_.Guest.IPAddress -eq '1.1.1.2'}

NOTE: I found that this only works on PowerCLI 6.3, and doesn't work on PowerCLI 5.8. 注意:我发现这仅适用于PowerCLI 6.3,并且不适用于PowerCLI 5.8。 This may be why OP is not getting any results for the 'IP'. 这可能就是为什么OP没有获得“IP”的任何结果。

PowerCLI 5.8 (IP field empty): PowerCLI 5.8(IP字段为空): PowerCLI 5.8

PowerCLI 6.3 (IP field populated): PowerCLI 6.3(已填充的IP字段): PowerCLI 6.3

Finally found a way to use Get-View AND search across VMs with multiple IPs (including IPv6): 最后找到了一种在多个IP(包括IPv6)的虚拟机上使用Get-View和搜索的方法:

$ip = "192.168"
$list = get-view -ViewType VirtualMachine
$list | ? {$_.guest.net.IpAddress  -match $ip } | select name, @{N='IP';E={[string]::Join(',',$_.Guest.net.IPAddress)}}

在此输入图像描述

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

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