Skip to content

Commit 5ddf2f9

Browse files
committed
Merge branch 'feat_ImproveGetVNVMByAddress' into dev
2 parents 33e5228 + 1ef831f commit 5ddf2f9

File tree

3 files changed

+80
-14
lines changed

3 files changed

+80
-14
lines changed

ChangeLog.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
### ChangeLog for vNugglets.Utility PowerShell module
22

3+
#### v1.next, not yet released
4+
5+
- \[improvement] updated function `Get-VNVMByAddress`:
6+
- greatly increased speed by using `VMware.Vim.SearchIndex` for by-IP searches (does support wildcard, though -- by wildcard still uses slower mechanism); example speed increase: went from 13s to about 0.25s in a vCenter with about 7,500 VMs -- woo-hoo!
7+
- added new parameters `-GuestHostname` and `-UUID` to support for searching by guest DNS name or VM BIOS UUID (these also use a method of `VMware.Vim.SearchIndex`, so the searches are super fast)
8+
9+
310
#### v1.1, released 20 Dec 2016
411

512
- \[new] added function `Find-VNVMWithDuplicateMACAddress` for finding duplicate VM NIC MAC address in vCenter
@@ -10,13 +17,13 @@
1017
- created module from many of the juicy snippets/functions that we shared at [vNugglets.com](http://vNugglets.com) over the years
1118
- updated `Copy-VNVIRole` to be a safer function overall by removing old `Invoke-Expression` methodology
1219
- standardized parameter names across cmdlets in the module and expanded some previously truncated/cryptic parameter names (go, usability and discoverability!)
13-
- added/updated "by name regular expression pattern" and "by liternal name string" parameters to several cmdlets
20+
- added/updated "by name regular expression pattern" and "by literal name string" parameters to several cmdlets
1421
- modernized cmdlets to use capabilities of somewhat newer PowerShell releases (like ordered hashtables) and built-in property return iteration, breaking PowerShell v2.0 compatibility (it's time to upgrade, right?)
15-
- updated `Invoke-VNEvacuateDatastore`:
22+
- updated `Invoke-VNEvacuateDatastore`:
1623
- added feature that uses any/all datastores in datastore cluster (when specifying a datastore cluster for Destination parameter value) for potential destination _per object_ (this allows for a potentially different datastore for each virtual disk on a VM)
1724
- added ability to exclude a VM/template's files from evacuation process (via parameter)
1825
- added `-WhatIf` support
19-
- updated cmdlet names to use standard/approved verbs where they were not already in use. Renamed functions/snippets as follows:
26+
- updated cmdlet names to use standard/approved verbs where they were not already in use. Renamed functions/snippets as follows:
2027
- `Get-VNVMByRDM` was "Get-VMWithGivenRDM"
2128
- `Get-VNVMByVirtualPortGroup` was "Get-VMOnNetworkPortGroup"
2229
- `Get-VNVMHostBrokenUplink` was "Get-BustedVmnic"

ToDo.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
- support for virtual distributed switches ("vDS" or "vDSwitches"); currently only supports virtual standard switches ("vSS")
66
- support for accepting VMHost ID from pipeline (by property), so that the following works tip top:
77
`Get-Cluster myCluster | Get-VMHost | Get-VNVMHostBrokenUplink`
8-
- update function `Get-VNVMByAddress` to:
9-
- use `VMware.Vim.SearchIndex` for speed for FindAllByIp (does support wildcard, though)
10-
- support searching by guest DNS name (also using `VMware.Vim.SearchIndex`, via the `FindAllByDnsName` method)
118
- update function `Get-VNVMByVirtualPortGroup` to take a standard- or distributed virtual portgroup as a parameter (and, from pipeline)
129
- update function `Copy-VNVIRole` to take SourceVIRole as VIRole object, and to accept it from pipeline
1310
- update function `Get-VNUplinkNicForVM` to give more meaningful VDSwitch identification (currently returns something to the effect of `DvsPortset-1` for the VDSwitch name)

vNugglets.Utility/vNuggletsUtilityMod_functions.ps1

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ function Get-VNVMByAddress {
227227
228228
.Example
229229
Get-VNVMByAddress -IP 10.37.31.12
230-
Name IP MoRef
231-
------ --- -----
232-
myvm10 {192.16.13.1, 10.37.31.12, fe80::000...} VirtualMachine-vm-13
230+
Name IP MoRef Client
231+
------ --- ----- ------
232+
myvm10 {192.16.13.1, 10.37.31.12, fe80::000...} VirtualMachine-vm-13 VMware.Vim.VimClientImpl
233233
234234
Get VMs with given IP as reported by VMware Tools, return VM name and its IP addresses
235235
@@ -242,13 +242,29 @@ function Get-VNVMByAddress {
242242
243243
Use -AddressWildcard to find VMs with approximate IP
244244
245+
.Example
246+
Get-VNVMByAddress -IP 10.37.31.12
247+
Name GuestHostname MoRef Client
248+
------ ------------- ----- ------
249+
myvm10 myvm10.dom.com VirtualMachine-vm-13 VMware.Vim.VimClientImpl
250+
251+
Get VMs with given hostname configured in the guest OS as reported by VMware Tools, return VM name and its guest hostname
252+
253+
.Example
254+
Get-VNVMByAddress -Uuid b99b546a-ee00-43f3-856a-80779ffddd0e
255+
Name Uuid MoRef Client
256+
------ ---- ----- ------
257+
myvm37 b99b546a-ee00-43f3-856a-80779ffddd0e VirtualMachine-vm-19991 VMware.Vim.VimClientImpl
258+
259+
Get VMs with given SMBIOS UUID, return VM name and its UUID
260+
245261
.Link
246262
Get-VNVMByRDM
247263
Get-VNVMByVirtualPortGroup
248264
http://vNugglets.com
249265
250266
.Notes
251-
Finding VMs by IP address relies on information returned from VMware Tools in the guest, so those must be installed in the guest and have been running in the guest at least recently.
267+
Finding VMs by IP address / Guest hostname relies on information returned from VMware Tools in the guest, so VMware Tools must be installed in the guest and have been running in the guest at least recently.
252268
253269
.Outputs
254270
Selected.VMware.Vim.VirtualMachine
@@ -263,21 +279,67 @@ function Get-VNVMByAddress {
263279
[parameter(Mandatory=$true,ParameterSetName="FindByIP",Position=0)][ValidateScript({[bool][System.Net.IPAddress]::Parse($_)})][string]$IP,
264280

265281
## wildcard string IP address (standard wildcards like "10.0.0.*"), if finding VM by approximate IP
266-
[parameter(Mandatory=$true,ParameterSetName="FindByIPWildcard",Position=0)][string]$AddressWildcard
282+
[parameter(Mandatory=$true,ParameterSetName="FindByIPWildcard",Position=0)][string]$AddressWildcard,
283+
284+
## Fully qualified DNS hostname as it appears in guest OS, for finding VM by guest hostname
285+
[parameter(Mandatory=$true,ParameterSetName="FindByGuestHostname",Position=0)][string]$GuestHostname,
286+
287+
## VM SMBIOS UUID, for finding VM by UUID
288+
[parameter(Mandatory=$true,ParameterSetName="FindByUuid",Position=0)][string]$Uuid
267289
) ## end param
268290

291+
begin {
292+
## array of properties to select on VirtualMachine object return when searching by IP or IP Wildcard, by Guest hostname, etc.
293+
$arrPropertiesForReturnWhenSearchByIP = "Name", @{n="IP"; e={$_.Guest.Net | Foreach-Object {$_.IpAddress} | Sort-Object}}, "MoRef", "Client"
294+
$arrPropertiesForReturnWhenSearchByGuestHostname = "Name", @{n="GuestHostname"; e={$_.Guest.HostName}}, "MoRef", "Client"
295+
$arrPropertiesForReturnWhenSearchByUuid = "Name", @{n="Uuid"; e={$_.Config.Uuid}}, "MoRef", "Client"
296+
} ## end begin
297+
269298
Process {
270299
Switch ($PsCmdlet.ParameterSetName) {
271300
"FindByMac" {
272301
## return the some info for the VM(s) with the NIC w/ the given MAC
273302
Get-View -Viewtype VirtualMachine -Property Name, Config.Hardware.Device | Where-Object {$_.Config.Hardware.Device | Where-Object {($_ -is [VMware.Vim.VirtualEthernetCard]) -and ($MAC -contains $_.MacAddress)}} | Select-Object Name, @{n="MacAddress"; e={$_.Config.Hardware.Device | Where-Object {$_ -is [VMware.Vim.VirtualEthernetCard]} | Foreach-Object {$_.MacAddress} | Sort-Object}}, MoRef
274303
break
275304
} ## end case
276-
{"FindByIp","FindByIPWildcard" -contains $_} {
305+
"FindByIp" {
306+
## get the SearchIndex object(s) (one from each connected vCenter)
307+
Get-View -Id SearchIndex-SearchIndex -PipelineVariable viewThisSearchIndex | Foreach-Object {
308+
## the vCenter name for this SearchIndex (ServiceUrl is like "https://myvcenter.dom.com/sdk", and the .Host property of a .NET URI object is just the DNS hostname portion of the URI)
309+
$strVcenterOfThisSearchIndex = ([System.Uri]$viewThisSearchIndex.Client.ServiceUrl).Host
310+
## SearchIndex Find* methods return MoRefs; docs at http://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.wssdk.apiref.doc/vim.SearchIndex.html
311+
# FindAllByIp(moref datacenter*, string ip, bool vmSearch)
312+
$viewThisSearchIndex.FindAllByIp($null, $IP, $true) | Select-Object -Unique | Foreach-Object {Get-View -Id $_ -Property Name, Guest.Net -Server $strVcenterOfThisSearchIndex} | Select-Object -Property $arrPropertiesForReturnWhenSearchByIP
313+
} ## end foreach-object
314+
break
315+
} ## end case
316+
"FindByGuestHostname" {
317+
## get the SearchIndex object(s) (one from each connected vCenter)
318+
Get-View -Id SearchIndex-SearchIndex -PipelineVariable viewThisSearchIndex | Foreach-Object {
319+
## the vCenter name for this SearchIndex (ServiceUrl is like "https://myvcenter.dom.com/sdk", and the .Host property of a .NET URI object is just the DNS hostname portion of the URI)
320+
$strVcenterOfThisSearchIndex = ([System.Uri]$viewThisSearchIndex.Client.ServiceUrl).Host
321+
## SearchIndex Find* methods return MoRefs; docs at http://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.wssdk.apiref.doc/vim.SearchIndex.html
322+
# FindAllByDnsName(moref datacenter*, string dnsName, bool vmSearch)
323+
$viewThisSearchIndex.FindAllByDnsName($null, $GuestHostname, $true) | Select-Object -Unique | Foreach-Object {Get-View -Id $_ -Property Name, Guest.HostName -Server $strVcenterOfThisSearchIndex} | Select-Object -Unique -Property $arrPropertiesForReturnWhenSearchByGuestHostname
324+
} ## end foreach-object
325+
break
326+
} ## end case
327+
"FindByUuid" {
328+
## get the SearchIndex object(s) (one from each connected vCenter)
329+
Get-View -Id SearchIndex-SearchIndex -PipelineVariable viewThisSearchIndex | Foreach-Object {
330+
## the vCenter name for this SearchIndex (ServiceUrl is like "https://myvcenter.dom.com/sdk", and the .Host property of a .NET URI object is just the DNS hostname portion of the URI)
331+
$strVcenterOfThisSearchIndex = ([System.Uri]$viewThisSearchIndex.Client.ServiceUrl).Host
332+
## SearchIndex Find* methods return MoRefs; docs at http://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.wssdk.apiref.doc/vim.SearchIndex.html
333+
# FindAllByUuid(moref datacenter*, string uuid, bool vmSearch, bool instanceUuid*)
334+
$viewThisSearchIndex.FindAllByUuid($null, $Uuid, $true, $false) | Select-Object -Unique | Foreach-Object {Get-View -Id $_ -Property Name, Config.Uuid -Server $strVcenterOfThisSearchIndex} | Select-Object -Unique -Property $arrPropertiesForReturnWhenSearchByUuid
335+
} ## end foreach-object
336+
break
337+
} ## end case
338+
"FindByIPWildcard" {
277339
## scriptblock to use for the Where clause in finding VMs
278-
$sblkFindByIP_WhereStatement = if ($PsCmdlet.ParameterSetName -eq "FindByIPWildcard") {{$_.IpAddress | Where-Object {$_ -like $AddressWildcard}}} else {{$_.IpAddress -contains $IP}}
340+
$sblkFindByIP_WhereStatement = {$_.IpAddress | Where-Object {$_ -like $AddressWildcard}}
279341
## return the .Net View object(s) for the VM(s) with the NIC(s) w/ the given IP
280-
Get-View -Viewtype VirtualMachine -Property Name, Guest.Net | Where-Object {$_.Guest.Net | Where-Object $sblkFindByIP_WhereStatement} | Select-Object Name, @{n="IP"; e={$_.Guest.Net | Foreach-Object {$_.IpAddress} | Sort-Object}}, MoRef
342+
Get-View -Viewtype VirtualMachine -Property Name, Guest.Net | Where-Object {$_.Guest.Net | Where-Object $sblkFindByIP_WhereStatement} | Select-Object -Property $arrPropertiesForReturnWhenSearchByIP
281343
} ## end case
282344
} ## end switch
283345
} ## end process

0 commit comments

Comments
 (0)