I was working on a request where the user wanted to find the details of the Virtual Machines within the vCenter Server.
What other better tool than PowerCLi to collect information about the inventory within the vCenter Server. Hence the title, PowerCLI Find Virtual Machines Details.
Below was the requirement that had to be collected in regards to the Virtual Machine:
- Name of The Virtual Machine.
- Folder location.
- Allocated CPU.
- Allocated Memory.
- Consumed CPU.
- Consumed Memory.
- Provisioned Storage.
- Used Storage.
- Current Power State of the Virtual Machine.
- Host on which the VM is residing.
- Datastore on which the VM is residing.
- Network Connection.
- IP Address.
I knew that I would get most of the information using the Get-VM cmdlet from the PowerCLI console except a few of the important parameters.
With that being said, below is the script I wrote and it worked like a charm.
Connect-VIServer 192.168.1.11
$VMs = Get-VM
$Output = foreach ($VM in $VMs){
Get-VM $VM | select Name, @{N="FolderName";E={ $_.Folder.Name}}, NumCpu, MemoryGB, @{N="CPUUsage";E={ $_.ExtensionData.Summary.QuickStats.OverallCpuUsage}}, @{N="MemoryUsage";E={$_.ExtensionData.Summary.QuickStats.GuestMemoryUsage}}, ProvisionedSpaceGB, UsedSpaceGB, PowerState, VMHost, @{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}}, @{N="Network";E={$_.Guest.Nics[0]}}, @{N="IPAddress";E={$_.Guest.IPAddress[0]}}, @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}}
}
$Output | Export-Csv C:\Result.csv -NoTypeInformation
Now, let us go over the lines and understand a couple of things.
In the first line, we use Connect-VIserver cmdlet to make a connection either to the vCenter Server or an ESXi host.
Second, we assign all the VMs that are registered on the host (in case you connect to the host) or present in the vCenter inventory.
Then, we just applied a simple foreach loop for each VM that is part of the variable $VMs and selected the properties that were originally required.
Finally, we just exported the output to a CSV file for easy consumption and shareability.
To run, open the PowerCLI console in Administrator mode. This will load all the necessary modules that are part of the console.
Navigate to the directory where you have saved the above script and execute.
The output of the CSV file will look as seen below.
I hope this has been informative and thank you for reading!