Thanks LucD, I think I get it now, I should be able to add all the other fields solo.
Original Message:
Sent: Nov 01, 2024 01:46 PM
From: LucD
Subject: get VM inventory using get-view
Try like this
Get-View -ViewType VirtualMachine -Property Parent, Config, Summary, Runtime |ForEach-Object -Process { $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent [PSCustomObject] @{ GuestId = $_.Config.GuestId vCenter = ([uri]$_.Client.ServiceUrl).Host Folder = (Get-View -Id $_.Parent -Property Name).Name 'IP Address' = $_.Summary.Guest.IpAddress Template = $_.summary.config.Template VMHost = $esx.Name Cluster = (Get-View -Id $esx.Parent -Property Name).Name }}
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Nov 01, 2024 11:29 AM
From: dbutch1976
Subject: get VM inventory using get-view
Last thing I need to add is folder. Try as I might to do it myself I can't seem to get it. Using James' suggestion I ran this to see if the folder was contained in the virtual machine viewtype:
get-view -viewtype VirtualMachine -filter @{Name="civ2"} | fc -depth 7
Could not find it, however I see that I can get folder information from:
get-view -viewtype Folder which contains the the info I need, but can't seem to figure out how I add this to the script. I've tried:
Get-View -ViewType VirtualMachine -Property Config,Summary,Runtime |
ForEach-Object -Process {
$esx = Get-View -Id $_.Runtime.Host -Property Name, Parent
$folder = Get-View -ViewType Folder
[PSCustomObject] @{
Name = $_.config.Name
'IP Address' = $_.Summary.Guest.IpAddress
Folder = $folder.name
GuestId = $_.Config.GuestId
Template = $_.summary.config.Template
vCenter = ([uri]$_.Client.ServiceUrl).Host
VMHost = $esx.Name
Cluster = (Get-View -Id $esx.Parent -Property Name).Name
Notes = $_.Summary.Config.Annotation
}
}|
Export-Csv -path C:\output\getview_comprehensive_inventory1.csv -NoTypeInformation
Feel like I'm on the right track, but getting this result for folder:
Name : VCNEO
IP Address : 192.168.0.12
Folder : {Datacenters, host, network, datastore...}
GuestId : other3xLinux64Guest
Template : False
vCenter : vcneo.lebrine.local
VMHost : grey.lebrine.local
Cluster : VSAN
Notes : VMware vCenter Server Appliance
Little help with this last piece?
Original Message:
Sent: Oct 31, 2024 05:25 PM
From: LucD
Subject: get VM inventory using get-view
Just pipe the output after the end of the Foreach-Object loop to an Export-Csv
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Oct 31, 2024 03:50 PM
From: dbutch1976
Subject: get VM inventory using get-view
What would be the best way to export this to csv? Or would it require a different approach entirely?
Original Message:
Sent: Oct 31, 2024 03:13 PM
From: LucD
Subject: get VM inventory using get-view
The issue might be that some properties are not directly found in the object representing the inventory object.
The vSphere Web Services API document is your basic reference to consult.
In your case, start with the VirtualMachine object, since you are looking at VMs.
It is worth knowing that most objects used in vSphere, are not flat objects.
They are often nested objects, i.e. a level 1 property points to another object, with it's own properties.
Back to your question, the VirtualMachine object has a Runtime property that points to a VirtualMachineRuntimeInfo object.
That object has a Host property, containing a HostSystem object pointer.
These pointers are called MoRef, which stands for Managed Object Reference.
With Get-View you can fetch the actual object from such a pointer.
And in the HostSystem object there is a Name
property, which is one of the values your looking for.
Get-View –ViewType VirtualMachine |Select-Object Name, @{N='GuestId';E={$_.Config.GuestId}}, @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}}, @{N='IP Address';E={$_.Summary.Guest.IpAddress}}, @{N='Template';E={$_.summary.config.Template}}, @{N='VMHost';E={(Get-View -Id $_.Runtime.Host).Name}}
To find the cluster, you can use the Parent property of the HostSystem object.
Get-View –ViewType VirtualMachine -Filter @{Name='TestVM3'}|Select-Object Name, @{N='GuestId';E={$_.Config.GuestId}}, @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}}, @{N='IP Address';E={$_.Summary.Guest.IpAddress}}, @{N='Template';E={$_.summary.config.Template}}, @{N='VMHost';E={(Get-View -Id $_.Runtime.Host).Name}}, @{N='Cluster';E={(Get-View -Id (Get-View -Id $_.Runtime.Host).Parent).Name}}
Note that the Cluster value will not work when the ESX node is not part of a Cluster.
Now this code can be optimised.
Limit the properties returned for an object by Get-View.
Don't run the same code twice.
In the end you can end up with something like this
Get-View -ViewType VirtualMachine -Property Config,Summary,Runtime |ForEach-Object -Process { $esx = Get-View -Id $_.Runtime.Host -Property Name, Parent [PSCustomObject] @{ GuestId = $_.Config.GuestId vCenter = ([uri]$_.Client.ServiceUrl).Host 'IP Address' = $_.Summary.Guest.IpAddress Template = $_.summary.config.Template VMHost = $esx.Name Cluster = (Get-View -Id $esx.Parent -Property Name).Name }}
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Oct 31, 2024 01:24 PM
From: dbutch1976
Subject: get VM inventory using get-view
Hello,
I've been playing with get-view rather than get-vm and I've been finding it to contain so much more information. Problem is I'm still struggling to find some fields which I could easily find in get-vm. I'm sure someone (probably LucD :) ) can instantly tell me where these are, but my real question is how do I find this information myself? Up until now I've been filtering on a single VM, then cycling through the various fields until I finally find what I'm looking for. Is there a way to search through all the fields when I know what I'm looking for?
The fields I'm trying to find are:
cluster, and VMhost
Here's have I've gotten so far:
get-view –viewtype VirtualMachine |
Select-Object Name,
@{N='GuestId';E={$_.Config.GuestId}},
@{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}},
@{N='IP Address';E={$_.Summary.Guest.IpAddress}},
@{N='Template';E={$_.summary.config.Template}}