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
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
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
I am pretty new at this. How do I run the script below.
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'
I get the following error when I run it in the cli window
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: ( , ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.GetVM
Never mind. The first cluster it found was an empty cluster which made all the values nul