VMware Cloud Community
2448Charles
Enthusiast
Enthusiast
Jump to solution

Get List of VMs, Datastores and Folder name per Cluster


Hello!!  I'm hoping someone can assist me with a PowerCLI script that can pull the subject info into a txt or csv file.  In an effort to get our environment cleaned up before our next DR exercise with SRM, I want to get a list that shows each VM, the datastore(s) it resides on, and the Folder name within VMs and Templates view.  We keep our VMs organized by creating folder names that correspond to the app running on the VM.  I mention SRM because last year our Protected Groups matched the Rcopy groups from our 3PAR array.  However, we've added over 200 new VMs since that time and my team members basically placed these new VMs on the first available datastore that had enough free space.  Unfortunately, we're not utilizing Storage Profiles...it's scheduled for next year.

The extracted info would look similiar to the following:

Servername    Datastore   Folder Name

Server1            LUN0           ActiveDirectory

Server2            LUN0           ActiveDirectory

Server3            LUN1           ActiveDirectory

Server4            LUN3           Argo

Server5            LUN7           Argo

Server6            LUN6           Lockbox

Server7            LUN5           Lockbox

Server8            LUN9           Citrix

etc...

Any help with extracting this info from vCenter would be greatly appreciated.

Thanks,

Charles

98 Replies
LucD
Leadership
Leadership
Jump to solution

thumbs-up.png


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Hi Luc

I'd like to improve the script: orderding the output by folder name.

I tried to add this " | Sort-Object -descending " at the end of " @{N="Folder";E={$_.Folder.Name}} "

But I didn't worked out

Any idea?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM |

  Select Name,

@{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}},

@{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},

@{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},

@{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},

@{N = "Folder"; E = {$_.Folder.Name}} |

  Sort-Object -Property Folder


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

tkdreamer2
Enthusiast
Enthusiast
Jump to solution

It works! I was almost there!

Thank you Luc

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Hello LucD

I know this work: Get-VM vmname | Get-NetworkAdapter | Select -ExpandProperty MacAddress

But, do you know how to put it to the script to also get the MacAddress of each vm?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Create an additional calculated property for that.

I added the -join in case you have VMs with more than 1 vNIC.

Get-VM |

Select Name,

   @{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}},

   @{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},

   @{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},

   @{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},

   @{N = "Folder"; E = {$_.Folder.Name}},

   @{N = 'MAC';E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}} |

Sort-Object -Property Folder


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

tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Thank you!

It works fine

I was able to notice that someone cloned a vm and I had 2 identicals mac addresses - -' and to see several mac addresses for a single vm...

In fact I had alarm on duplicated MacAddresses but this KB VMware Knowledge Base couldn't help me because my vCenters do not have the same ID..

0 Kudos
DanMan3395
Enthusiast
Enthusiast
Jump to solution

Can you elaborate on the [string]::Join syntax? I get that you are calling a .net string.join method but what are you joining here and why?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The reason is because we want to have a single value in this calculated property.
So if the VM is located on several datastores, we join the names of the datastores (the 2nd operand to the Join method) with the 1 st operand character.


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

DanMan3395
Enthusiast
Enthusiast
Jump to solution

oh gotcha, so if more than one item is returned it joins them into a comma delineated list. Thanks man!

0 Kudos
phatq
Contributor
Contributor
Jump to solution

Is it also possible to list the VM's on certain datastores. I want to look for those VM's who resides on datastores with for instance "NACL" in de datastore name, and to also show me the corresponding VM folder where the VM's resides in. Thank you in advance!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Use the Datastore parameter and OBN.

Like this

Get-VM -Datastore "*NACL*" |

Select Name,

   @{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}},

   @{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},

   @{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},

   @{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},

   @{N = "Folder"; E = {$_.Folder.Name}},

   @{N = 'MAC';E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}} |

Sort-Object -Property Folder


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

0 Kudos
phatq
Contributor
Contributor
Jump to solution

I get this after using the script:

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you get the same error when you just do Get-VM?


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

0 Kudos
phatq
Contributor
Contributor
Jump to solution

No I don't, I then get a list of all the VM's. But most of the mentioned criteria in the query I don't need, like UsedSpace/MAC/ProvisionedSPace. I only need the VM name, Datastore name and the VM folder it resides in. If we were te exclude this, is the below query then valid. Also to view this in a comma delimited fashion with three columns next to eachother:

Get-VM -Datastore "*NACL*" |

Select Name,

   @{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}}

   @{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},

   @{N = "Folder"; E = {$_.Folder.Name}} |

Sort-Object -Property Folder

0 Kudos
phatq
Contributor
Contributor
Jump to solution

I forgot to mention to also add ClusterName to the query, this would also be of good use.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using?
There is no point in adapting the selection if you can't get the Get-VM working.


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

0 Kudos
phatq
Contributor
Contributor
Jump to solution

Version 6.3

pastedImage_0.png

Get-VM does work, I get a list of the VM's.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would suggest you upgrade your PowerCLI installation.


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

0 Kudos
phatq
Contributor
Contributor
Jump to solution

Thank you Luc, will do!

0 Kudos