From time to time, I get tasked with generating a report of production VMs with specs. This normally wouldn’t be an issue, but we have changed our server naming standard a few times, so we have machines with all sorts of names. The one thing in out environment that hasn’t changed is the datastore naming. We have our datastores split up between non-production and production, and each department has their own. So to get the production VMs, I can do a filter on the name of the datastores to get only the production one.
Get-datastore | Where {$_.name -like '*PROD*' -or $_.name -like '*REPL*'} | Get-VM
The script results are good, but I don’t need to know if the servers are powered on for the report that I’ve been tasked with. There are also several other options that I need to collect on the VMs before I can call my report finished. I need to get the folder that the VM is in along with the VM cluster, and network adapter
Get-datastore | Where {$_.name -like '*PROD*' -or $_.name -like '*REPL*'} | Get-VM | Select Name, @{N="vCPU";E={($_).NumCpu}}, @{N="Memory (GB)";E={($_).MemoryGB}}, @{N="Cluster";E={Get-Cluster -VM $_}}, @{N="Folder";E={$_.folder}}, @{N="Network";E={$_.Networkadapters.NetworkName}} | Format-Table
Now this is more like what I was tasked with collecting from vSphere. Now that I have the neccessary information, I need to export it to an CSV.
$report = @() $VMs = Get-datastore | Where {$_.name -like '*PROD*' -or $_.name -like '*REPL*'} | Get-VM Foreach ($VM in $VMs){ $line = $VM | Select Name, @{N="vCPU";E={($_).NumCpu}}, @{N="Memory (GB)";E={($_).MemoryGB}}, @{N="Cluster";E={Get-Cluster -VM $_}}, @{N="Folder";E={$_.folder}}, @{N="Network";E={$_.Networkadapters.NetworkName}} $report += $line } $report | Export-csv C:\scripts\logs\ProductionVMs.csv -NoTypeInformation -UseCulture
There are many different specs that can be collected with this script. I also have a version that states whether or not the systems has RDMs attached. This is very helpful when needing to move the VM between cluster, as I would need to have the storage team to attach the RDMs to the new destination cluster.
$report = @() $VMs = (Get-Datastore | Where {$_.Name -like '*REPL*' -or $_.Name -like '*PROD*'} | Get-VM).Name | sort Foreach ($VM in $VMs){ $line = Get-VM $VM | Select Name, @{N="vCPU";E={($_).NumCpu}}, @{N="Memory (GB)";E={($_).MemoryGB}}, @{N="Cluster";E={Get-Cluster -VM $_}}, @{N="Folder";E={$_.folder}}, @{N="Network";E={(Get-NetworkAdapter -VM $_).NetworkName}}, @{Expression={if (($_ | Get-HardDisk | Where {$_.DiskType -eq "RawPhysical"}) -eq $Null) {"No"} Else {"Yes"}}; Label="RDMs" } $report += $line } $report | Export-Csv C:\scripts\logs\ProductionVMs.csv -NoTypeInformation -UseCulture
– Stuart
Find this script on Github