VMware Cloud Community
angoletti1
Contributor
Contributor
Jump to solution

HowTo get all VMs of a specified datastores?

Hi,

here what I want:

I want a list of all VMs, which are running on datastores, which names start with 02.

I tried with get-datastore where name 02* | get-vm , but this didn't work.

Regards

angoletti1

Reply
0 Kudos
1 Solution

Accepted Solutions
kjb007
Immortal
Immortal
Jump to solution

Get-VM -Datastore (Get-Datastore -Name 02*) |select-object -property Name |format-table -hidetableheaders > FileName.txt

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like

Get-VM -Datastore (Get-Datastore -Name 02*)


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

Reply
0 Kudos
Niket
Enthusiast
Enthusiast
Jump to solution

Hi angoletti1

Please refer the below script for the same

$server = get-viserver -server #To connect to server

$datastore = Get-Datastore -Name "02*" #To get the Datastore starts with 02

Get-VM -Datastore $datastore # Get all the VM's in the datastores starts with 02

I hope it resolves your issue. Please let me know if you have any issues in running the script.

Thanks

-Niket

Reply
0 Kudos
angoletti1
Contributor
Contributor
Jump to solution

hey, very good, that helps Smiley Happy

Now one additional small thing: I want only see the name of the vm and write all this to a file.

Reply
0 Kudos
kjb007
Immortal
Immortal
Jump to solution

Get-VM -Datastore (Get-Datastore -Name 02*) |select-object -property Name > FileName.txt

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
Niket
Enthusiast
Enthusiast
Jump to solution

Hi

To view only the name just

$server = get-viserver -server 192.168.111.13 -user root -password esxadmin

$datastore = Get-Datastore -Name "02*"

Get-VM -Datastore $datastore | ft name # add ft name it will only display the names

To export in a file use the below code snippet

Get-VM -Datastore $datastore | Export-Csv -Path "C:\Data.csv" # it will display all the information about the VM's

I hope it resolves your issue

Thanks

-Niket

angoletti1
Contributor
Contributor
Jump to solution

perfect, I'm very close to what I want.

The only thing is, that in the txt I see "Name", but I only want the server names with nothing else.

Reply
0 Kudos
kjb007
Immortal
Immortal
Jump to solution

Get-VM -Datastore (Get-Datastore -Name 02*) |select-object -property Name |format-table -hidetableheaders > FileName.txt

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
Reply
0 Kudos