Over time I have produced a number of powershell scripts using the vSphere PowerCLI snap-in to return information from vCenter
Firstly, I looked into returning inventory information for all the ESXi Hosts where the following would be retrieved:
- Name
- Hardware Vendor
- Hardware Model
- CPU Model
- Datacenter
- Cluster
- Hypervisor
- Hypervisor Version
- Clock Speed
- Memory
- Hyperthreading Active
- Number of Cores
In order to invoke the PowerCLI cmdlets we will need to add the snap-ins to the current powershell session and connect to the vCenter server (in the below example this is named ‘server.domain.local’).
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) { Add-PSSnapin VMware.VimAutomation.Core > $null } Connect-VIServer server.domain.local
Now, in order to retrieve information for each ESXi host we will build a collection by invoking the Get-VMHost cmdlet
$VMHosts = Get-VMHost
From the ESXi hosts returned in the collection we will loop through each one and return the required information as above and return this as calculated properties and store in a variable to export later to a CSV file.
$Inventory = ForEach ($VMHost in $VMHosts) { "" | Select-Object -Property @{N="Name";E={$VMHost.Name}}, @{N="Vendor";E={(Get-View -ViewType HostSystem -Filter @{"Name" = $VMHost.Name}).Hardware.Systeminfo.Vendor}}, @{N="Model";E={(Get-View -ViewType HostSystem -Filter @{"Name" = $VMHost.Name}).Hardware.Systeminfo.Model}}, @{N="CPU Model";E={$VMHost.ExtensionData.Summary.Hardware.CpuModel}}, @{N="Datacenter";E={(Get-Datacenter -VMHost $VMHost.Name).Name}}, @{N="Cluster";E={(Get-Cluster -VMHost $VMHost.Name).Name}}, @{N="Hypervisor";E={$VMHost.Extensiondata.Config.Product.Name}}, @{N="Hypervisor Version";E={$VMHost.Extensiondata.Config.Product.Version}}, @{N="Clock Speed (Mhz)";E={$VMHost.ExtensionData.Summary.Hardware.CpuMhz}}, @{N="Memory (MB)";E={$VMHost.MemoryTotalMB}}, @{N="Hyperthreading Enabled";E={$VMHost.HyperThreadingActive}}, @{N="Number of Cores";E={$VMHost.ExtensionData.Summary.Hardware.numCpuCores}} }
As I want the output to create a unique filename each time the inventory script is invoked to the output folder D:\Output, therefore I will use the Guid.NewGuid method when specifying a filename.
$Inventory | Export-Csv -NoTypeInformation -Path ("D:\Output\" + [guid]::NewGuid() + "-inv_hosts.csv")
Below is a table to include the cmdlets required to extract the inventory information in the requirements.
Name | Cmdlet | Property |
Name | Get-VMHost | Name |
Hardware Vendor | Get-View | Hardware.Systeminfo.Vendor |
Hardware Model | Get-View | Hardware.Systeminfo.Model |
CPU Model | Get-VMHost | ExtensionData.Summary.Hardware.CpuModel |
Datacenter | Get-Datacenter | Name |
Cluster | Get-Cluster | Name |
Hypervisor | Get-VMHost | ExtensionData.Config.Product.Name |
Hypervisor Version | Get-VMHost | ExtensionData.Config.Product.Version |
Clock Speed (Mhz) | Get-VMHost | ExtensionData.Summary.Hardware.CpuMhz |
Memory (MB) | Get-VMHost | MemoryTotalMB |
Hyperthreading Active | Get-VMHost | HyperThreadingActive |
Number of Cores | Get-VMHost | ExtensionData.Summary.Hardware.numCpuCores |
very nice Powershell, comes in handy!
LikeLike