VMware Cloud Community
larryW201110141
Contributor
Contributor
Jump to solution

Script to List all of the Datastores in a cluster, the VMs on the datastore and the host the vm is on

We have datastores that are seen by more than one cluster. I need a script that will list:

all the datastores seen by a cluster

the vms on the datastore

the host the vm is on

the cluster the the host is in

The output I would like is

datastore name, VM name, Host Name, Cluster Name

any help would be appreciated

Reply
0 Kudos
1 Solution

Accepted Solutions
aerodevil
Hot Shot
Hot Shot
Jump to solution

Pretty sure this will do what you need:

$report = @()
Foreach($cluster in Get-Cluster){
    $datastores = $cluster | Get-VMHost | Get-Datastore
    foreach($datastore in $datastores){
    $vms = $datastore | Get-VM
    If ($vms.count -ge 1){
        foreach($VM in $vms){
        $object = New-Object -TypeName PSObject -Property @{
              Datastore = $datastore.Name
              VM = $VM.Name
              HostName = $VM.VMhost
              Cluster = $cluster
            }
        $report += $object
        }
   
    }
    }
}
$report | Export-Csv C:\Temp\VMs.csv -NoTypeInformation -UseCulture

If you only want Shared datastores then you could use

$datastores = $cluster | Get-VMHost | Get-Datastore | Where {$_.Extensiondata.Summary.MultipleHostAccess -eq $True}

Hope this helps

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/

View solution in original post

Reply
0 Kudos
5 Replies
aerodevil
Hot Shot
Hot Shot
Jump to solution

Pretty sure this will do what you need:

$report = @()
Foreach($cluster in Get-Cluster){
    $datastores = $cluster | Get-VMHost | Get-Datastore
    foreach($datastore in $datastores){
    $vms = $datastore | Get-VM
    If ($vms.count -ge 1){
        foreach($VM in $vms){
        $object = New-Object -TypeName PSObject -Property @{
              Datastore = $datastore.Name
              VM = $VM.Name
              HostName = $VM.VMhost
              Cluster = $cluster
            }
        $report += $object
        }
   
    }
    }
}
$report | Export-Csv C:\Temp\VMs.csv -NoTypeInformation -UseCulture

If you only want Shared datastores then you could use

$datastores = $cluster | Get-VMHost | Get-Datastore | Where {$_.Extensiondata.Summary.MultipleHostAccess -eq $True}

Hope this helps

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
Reply
0 Kudos
larryW201110141
Contributor
Contributor
Jump to solution

I am pretty new at this. How do I run the script below.

Reply
0 Kudos
aerodevil
Hot Shot
Hot Shot
Jump to solution

No problem.

This one works fine just copying and pasting it into your PowerCLI window.  I do recommend making sure you connect to your vCenter server first and setting the path and filename where you would want it.

You can also store this in a .ps1 file and dot source it.  It would look like this in your console.

xxPowerCLI> . 'C:\Path\To\PS1\script.ps1'
Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
larryW201110141
Contributor
Contributor
Jump to solution

I get the following error when I run it in the cli window

C:\scripts>

C:\scripts> $report = @()

C:\scripts> Foreach($cluster in Get-Cluster){

>> $datastores = $cluster | Get-VMHost | Get-Datastore

>> foreach($datastore in $datastores){

>> $vms = $datastore | Get-VM

>> If ($vms.count -ge 1){

>> foreach($VM in $vms){

>> $object = New-Object -TypeName PSObject -Property @{

>> Datastore = $datastore.Name

>> VM = $VM.Name

>> HostName = $VM.VMhost

>> Cluster = $cluster

>> }

>> $report += $object

>> }

>>

>> }

>> }

>> }

>> $report | Export-Csv C:\Temp\VMs.csv -NoTypeInformation -UseCulture

>>

Get-VM : Cannot validate argument on parameter 'Datastore'. The argument is null or empty. Supply an argument that is not null or empty and then try

the command again.

At line:4 char:31

+ $vms = $datastore | Get-VM <<<<

+ CategoryInfo : InvalidData: (Smiley Happy , ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.GetVM

Reply
0 Kudos
larryW201110141
Contributor
Contributor
Jump to solution

Never mind. The first cluster it found was an empty cluster which made all the values nul

Reply
0 Kudos