VMware Cloud Community
wardla
Contributor
Contributor
Jump to solution

Newbie Question: Listing VMs Configuration and What Datastores

Hi

I am new to using powercli How do I list all VMs in a certain datacenter, their memory, Procs and what datastores they reside on.

I know you can use get-vm | select-object name, NumCPU, MemoryMB  but if I add DatastoreIdList as well it doesnt seem to work.  What am I missing

Many Thanks

Laurence

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is correct, you could do

Get-VM MyVM | Select Name,@{N="DS";E={[string]::join(',',($_.datastoreidlist|%{(Get-View -Id $_).Name}))}},Description | 
    Export-Csv "C:\test.csv" -NoTypeInformation -UseCulture

Just watch out if the separator for the CSV happens to be a comma in your culture, then you will probably have to adapt the separator in the Join method!


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

First check which properties are available in a VM object.

Do

Get-VM MyVM | Get-Member

The DatastoreIdList property is an array of datastore Id (duh).

You can expand such a complex property with the ExpandProperty parameter

Get-VM MyVM | Select -ExpandProperty DatastoreIdList

But that doesn't allow you to combine this complex property with other properties.

That's where calculated properties come in.

Get-VM MyVM | Select Name,@{N="DS";E={[string]::join(',',($_.datastoreidlist|%{$_}))}}

But you would probably want to see the names of these datastores.

Then you have to use the ID to fetch the datastore object

Get-VM MyVM | Select Name,@{N="DS";E={[string]::join(',',($_.datastoreidlist|%{(Get-View -Id $_).Name}))}}

That looks complex, but if you know how it was constructed it's quite easy


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

wardla
Contributor
Contributor
Jump to solution

Many thansk for your reply.

If I wanted to also include description field and export this to to CSV.  Would I just use the | export-csv filename.csv -notypeinformation command

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct, you could do

Get-VM MyVM | Select Name,@{N="DS";E={[string]::join(',',($_.datastoreidlist|%{(Get-View -Id $_).Name}))}},Description | 
    Export-Csv "C:\test.csv" -NoTypeInformation -UseCulture

Just watch out if the separator for the CSV happens to be a comma in your culture, then you will probably have to adapt the separator in the Join method!


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

0 Kudos
wardla
Contributor
Contributor
Jump to solution

Hi,

Many thanks for a quick repsonse.  Really helpful.

How do you recommend I learn powercli

Regards

Laurence

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are several resources available. See also the blogroll in the official PowerCLI blog.

There's Hal's book, albeit written for an older release, still a good intro to PowerCLI.

There's the PowerCLI Reference, which is a bit more advanced and covers how to tackle all aspects of your vSphere environment with PowerCLI.

There is Appendix A from the PowerCLI Reference, that didn't make it into the book due to size constraints, that is available for free.

If you are looking for PowerShell intros, have a look at the resources mentioned in my post ,called My PS Library


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

0 Kudos