PowerCli to get all VMs on certain Datastores?

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
Output of script to get VMs on certain datastores
Output of script to get VMs on certain datastores

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
PowerCLI to get VMs based on Datastore report
Example report from the results of the Script to get VMs based on datastores

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

2 thoughts on “PowerCli to get all VMs on certain Datastores?”

    1. Joy,
      I have modified the script to capture the data that i believe that you are looking for.

      $report = @()
      $datastores = Get-datastore | Where {$_.name -like ‘*PROD*’ -or $_.name -like ‘*REPL*’}
      Foreach ($Datastore in $datastores){
      $VMs = $datastore | Get-VM
      Foreach ($VM in $VMs){
      $Line = “” | Select Name, vCPU, ‘Memory(GB)’, TotalHDD, Cluster, Folder, Datastore, ‘Datastore Capacity’
      $line.Name = $VM.name
      $line.vCPU = $vm.NumCpu
      $line.’Memory(GB)’ = $vm.MemoryGB
      $line.TotalHDD = ($VM | Get-HardDisk | Measure capacityGB -sum).sum
      $line.Cluster = Get-Cluster -VM $VM.name
      $line.Folder = $VM.Folder
      $line.Datastore = $datastore.name
      $line.’Datastore Capacity’ = $Datastore.CapacityGB
      $report += $line
      $report | ft -AutoSize

      I hope this helps.


Leave a Reply

Your email address will not be published. Required fields are marked *