VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

List more than one datastore

Hi

The following script works good except it doesn't show VM with more than one datastore:

Get-VM | Select Name,PowerState, @{N="SnapName";E={
($_ | Get-Snapshot).Name}},@{N="Datastore";E={
($_ | Get-Datastore).Name}},@{ N="TotalDisk"; E={ 
($_ | Get-harddisk | measure-object -property CapacityGB

-sum).Sum}},@{N="GuestOS";E={
($_ | Get-VMGuest).OSFullName}},@{N="Cluster";E={
($_ | Get-Cluster).Name}}

How can it be modified to list VM with more than one DS?

Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello,

@{N="Datastore";E={($_ | Get-Datastore | %{$_.Name}) -join ","}}

View solution in original post

Reply
0 Kudos
7 Replies
mattboren
Expert
Expert
Jump to solution

Hello,

@{N="Datastore";E={($_ | Get-Datastore | %{$_.Name}) -join ","}}

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Matt

Yes this works for me thanks.

I tried this but it didn't work {string]::join ','}). I guess I was close.

Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello-

Hmm.  Using the Join() method of the .NET String class should also work.  Like:


@{N="Datastore";E={[string]::join(",", ($_ | Get-Datastore).Name)}}

Does that not get it, too?

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Yes it does.

Thanks again

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Where would I add a filter for example:

Where-Object {$_.Name -like "Datastore*"}

Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello-

Well, depending on your desired outcome -- if you wanted all VMs and to only display datastore names for certain datastores, you would put such a filter inside of the calculated property for Datastore.

More likely, I would think, is that you are wanting to just get info about VMs that are on given datastores.  If that is the case, you can do the filtering by just getting the desired datastore(s) first, and then get the VMs that reside on them and do the Select-Object statement that you already have built.  So, for this, you would just put a Get-Datastore call at the start with the given datastore(s) name wildcard, and then pipe that to the rest of what you already have, like:


Get-Datastore -Name myDatastores* | Get-VM | Select ...

How about that?

AGFlora
Enthusiast
Enthusiast
Jump to solution

Exactly what I was looking for...

Thanks!

Reply
0 Kudos