VMware Cloud Community
mdangel1
Enthusiast
Enthusiast
Jump to solution

Get-VM | Select-Object question

All,

I am trying to write a script to get information on all my VM's. I have almost everything down packed, but I cant get the datastore. I want the out output to also show me what datastore the vm resides in.

Here is what I have so far.

Get-VM | Select-Object Name,VMHost,NumCPU,PowerState,MemoryMB 

Again, I need to be able to tell what datastore the vm resides in

0 Kudos
1 Solution

Accepted Solutions
mattandes
Enthusiast
Enthusiast
Jump to solution

Try this:

Get-VM | Select Name,VMHost,NumCPU,PowerState,MemoryMB,@{N="Datastore"; E={$_ | Get-Datastore}}

Blog: http://www.virtual-matt.net

View solution in original post

0 Kudos
7 Replies
MauroBonder
VMware Employee
VMware Employee
Jump to solution

Hi,

I´m not expert with powercli but i found this information: get vms that reside on datastore | VMware and Powershell

*Please, don't forget the awarding points for "helpful" and/or "correct" answers. *Por favor, não esqueça de atribuir os pontos se a resposta foi útil ou resolveu o problema.* Thank you/Obrigado
0 Kudos
mdangel1
Enthusiast
Enthusiast
Jump to solution

thank you, but it did not work. I am wondering if there is an option in the Select-Object

0 Kudos
MauroBonder
VMware Employee
VMware Employee
Jump to solution

Get List of VMs, Datastores and VMDK / path per Cluster

*Please, don't forget the awarding points for "helpful" and/or "correct" answers. *Por favor, não esqueça de atribuir os pontos se a resposta foi útil ou resolveu o problema.* Thank you/Obrigado
0 Kudos
mattandes
Enthusiast
Enthusiast
Jump to solution

Try this:

Get-VM | Select Name,VMHost,NumCPU,PowerState,MemoryMB,@{N="Datastore"; E={$_ | Get-Datastore}}

Blog: http://www.virtual-matt.net
0 Kudos
mdangel1
Enthusiast
Enthusiast
Jump to solution

Thank you!!!! that worked. Where could I have gotten that information? Can you recommend a book? That seems like a complex set of commands. You are the man

0 Kudos
mattandes
Enthusiast
Enthusiast
Jump to solution

The command is not that hard to understand and use once you understand what it's doing. Everything up to the @ sign is just selecting those properties to display them to the output. Below is what the rest of that line is doing.

@{N="Datastore";E={$_ | Get-Datastore}}

N="Datastore" - This creates another property to display in the output called "Datastore". This can be written longer as Name="Datastore".

E={$_ | Get-Datastore} - So the second part of this is an expression that gives you the value for the property "Datastore" in the output. This particular one takes the input from the pipe, in this case the Get-VM command, and then pipes that into the the Get-Datastore cmdlet which returns the Datastore that the VM resides on. This can be written longer as Expression={$_ | Get-Datastore}.

The rest of the brackets and symbols is just syntax stuff. As far as learning this stuff, I would suggest just getting into it using lots of Google and trying to learn by dissecting other people's code to learn neat little tricks to use for what you want to do.

Blog: http://www.virtual-matt.net
markdjones82
Expert
Expert
Jump to solution

Mdangel, I would suggest Luc/Alan's Powercli Reference book.  It is very good.  I would also agree with Matt to find tasks you are trying to do and instead of doing them in the GUI, practice them with Powercli and you will become more comfortable with it.

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos