Hi all
I want to list all VM's in datastores starting with "MDF". I've tried
Get-VM -Datastore (Get-Datastore -Name MDF*) |select-object -property Name, Datastore > VMplusdatastore.txt
But this doesn't list the datastore names for some reason ?
Also some VM's will have disks on more than one LUN - I guess this is not been reflected here ?
Would it be better to do it the other way around ?
Hi Simon,
the problem with your script is that the virtual machine object does not have a Datastore property. That is why the datastore column is empty in the output. The next script will combine the virtual machine names and the datastore for all the datastores with names starting with MDF:
Get-Datastore -Name MDF* | ForEach-Object { $Datastore = $_ $Datastore | Get-VM | ForEach-Object { $Report = "" | Select-Object -property Name, Datastore $Report.Name = $_.Name $Report.Datastore = $Datastore.name $Report } } > VMplusdatastore.txt
If a virtual machine has disks on multiple datastores it will show all the combinations.
Regards, Robert
Hi Simon,
the problem with your script is that the virtual machine object does not have a Datastore property. That is why the datastore column is empty in the output. The next script will combine the virtual machine names and the datastore for all the datastores with names starting with MDF:
Get-Datastore -Name MDF* | ForEach-Object { $Datastore = $_ $Datastore | Get-VM | ForEach-Object { $Report = "" | Select-Object -property Name, Datastore $Report.Name = $_.Name $Report.Datastore = $Datastore.name $Report } } > VMplusdatastore.txt
If a virtual machine has disks on multiple datastores it will show all the combinations.
Regards, Robert
Thats perfect - many thanks
I'm still having trouble working out how to use properties.
If I want to add disk size to this report, why doesn't this work;
Get-Datastore -Name MDF* | ForEach-Object {
$Datastore = $_
$Datastore | Get-VM | ForEach-Object {
$Report = "" | Select-Object -property Name, UsedSpaceGB, Datastore
$Report.Name = $_.Name
$Report.Datastore = $Datastore.name
$Report
}
} > VMplusdatastore.txt
Can anyone help with the correct way to do this ?
Try this
Get-Datastore -Name MDF* | ForEach-Object { $Datastore = $_
$Datastore | Get-VM | ForEach-Object { $Report = "" | Select-Object -property Name, UsedSpaceGB, Datastore
$Report.Name = $_.Name $Report.UsedSpaceGB = $_.UsedSpaceGB $Report.Datastore = $Datastore.name $Report } } > VMplusdatastore.txt
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ahh okay - stupid me
Many thanks!