VMware Cloud Community
bcsbrown
Contributor
Contributor
Jump to solution

Script to output VMs by Datastores

Hi All,

I'm new to these forums and to PowerCLI.  I'm trying to do something that I thought would be straight forward but I'm failing miserably.

I want to create a script that will take a list of specific datastores and output each specified datastore and the VMs that are on it.

Example Output:

Datastore          VM

--------------         ----------------

Datastore1        VM1

Datastore1        VM2

Datastore2        VM3

Datastore2        VM4

Example of a script that I've tried to create:

$Result=@()

$Datastores=Get-Datastore <Datastore1>,<Datastore2>

foreach ($Datastore in $Datastores)

{
$Result+=Get-datastore $Datastore | Get-VM | select @{N="Datastore";E={$Datastore.Name}},@{N="VM";E={$VM.Name}}
}

$Result

This is just one of many examples of scripts that I've tried and failed with.  Any help is VERY appreciated.  Thanks in advance!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

One way of doing that is like this

foreach($ds in Get-Datastore -Name datastore1,datastore2){
 
Get-VM -Datastore $ds |
 
Select Name,@{N="Datastore";E={$ds.Name}}
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

One way of doing that is like this

foreach($ds in Get-Datastore -Name datastore1,datastore2){
 
Get-VM -Datastore $ds |
 
Select Name,@{N="Datastore";E={$ds.Name}}
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
bcsbrown
Contributor
Contributor
Jump to solution

Thanks!  This worked like a charm!

Reply
0 Kudos