VMware Cloud Community
simonadams
Contributor
Contributor
Jump to solution

List VM's in Datastores

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 ?

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
5 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
simonadams
Contributor
Contributor
Jump to solution

Thats perfect - many thanks

0 Kudos
simonadams
Contributor
Contributor
Jump to solution

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 ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

simonadams
Contributor
Contributor
Jump to solution

Ahh okay - stupid me Smiley Happy

Many thanks!

0 Kudos