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
rama5591
Contributor
Contributor
Jump to solution

yes.

0 Kudos
rama5591
Contributor
Contributor
Jump to solution

The structure in Vcenter:

DataCenter

     Folder1   

               Cluster1    

                    Datastore1

                    Datastore2

               Cluster2

                    Datastore3

                    Datastore4

     Folder2

          Cluster2

                    Datastore5

                    Datastore6

Here I want to get Folder1 and Folder2 names.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-DatastoreCluster |

Select Name,

    @{N='Folder';E={

        $parent = Get-View -Id $_.ExtensionData.Parent

        while($parent -isnot [VMware.Vim.Folder]){

            $parent = Get-View -Id $parent.Parent

        }

        $parent.Name       

    }}


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

0 Kudos
rama5591
Contributor
Contributor
Jump to solution

Yes Great, Its working.

Thank you so much Smiley Happy

0 Kudos
Virtualduty
Enthusiast
Enthusiast
Jump to solution

Hi,

This script was very helpful , thank you !

Is there any parameter that can get the storage vendor details. I am looking for list of VMs, corresponding datastores for all the virtual disks, naa id and storage vendor name. We are using Hitachi, EMC and NetApp.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

One way of doing that would be through this calculated property.

  @{N='Vendor';E={(

    Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

    Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},


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

Virtualduty
Enthusiast
Enthusiast
Jump to solution

Works like a charm ! Thank you !

0 Kudos
Virtualduty
Enthusiast
Enthusiast
Jump to solution

I am also looking for provisioned space on all the VMs in these clusters. I ran the below script but the provisionspacegb column in the output file was all blank.

&{foreach($vm in Get-VM){&{foreach($vm in Get-VM){

Get-Datastore -RelatedObject $vm |

Select @{N='Cluster';E={Get-Cluster -VMHost $vm.VMhost | select -ExpandProperty Name}},

  Name,

  @{N='VMName';E={$vm.Name}},

  @{N='NAA';E={$_.ExtensionData.Info.Vmfs.Extent[0].DiskName}},

  @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},

  @{N="ProvisionedSpaceGB" ;E={(Get-VM -ProvisionedSpaceGB)}},

  @{N='Vendor';E={(

    Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

    Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},

  @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}}} |

Export-Csv  C:\Users\CA\vm-full-report-2.csv -NoTypeInformation -UseCulture

Could you help me understand what's wrong in my script ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are a few too many curly braces at the end

Get-Datastore -RelatedObject $vm |

Select @{N='Cluster';E={Get-Cluster -VMHost $vm.VMhost | select -ExpandProperty Name}},

  Name,

  @{N='VMName';E={$vm.Name}},

  @{N='NAA';E={$_.ExtensionData.Info.Vmfs.Extent[0].DiskName}},

  @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},

  @{N="ProvisionedSpaceGB" ;E={(Get-VM -ProvisionedSpaceGB)}},

  @{N='Vendor';E={(

    Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

    Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},

  @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}} |

Export-Csv  C:\Users\CA-VISM-ADM-P003\vm-full-report-2.csv -NoTypeInformation -UseCulture


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

0 Kudos
Virtualduty
Enthusiast
Enthusiast
Jump to solution

I ran below

&{foreach($vm in Get-VM){
Get-Datastore -RelatedObject $vm |

Select @{N='Cluster';E={Get-Cluster -VMHost $vm.VMhost | select -ExpandProperty Name}},

  Name,

  @{N='VMName';E={$vm.Name}},

  @{N='NAA';E={$_.ExtensionData.Info.Vmfs.Extent[0].DiskName}},

  @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},

  @{N="ProvisionedSpaceGB" ;E={(Get-VM -ProvisionedSpaceGB)}},

  @{N='Vendor';E={(

    Get-EsxCli -VMHost (Get-VMHost -Datastore $_ |

    Select -First 1)).storage.core.device.list($_.ExtensionData.Info.Vmfs.Extent[0].DiskName).Vendor}},

  @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}} |

Export-Csv  C:\Users\CA\vm-full-report-2.csv -NoTypeInformation -UseCulture

I get below error

Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You changed the script, now you need an extra { at the end of the Select (to close the &{)


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Thank you Luc! As usual: your work is amazing!

I know it's been a long time since your last answer but if I need to also get the OS version?

I can get it this way, but I can't put it in the precedant script:

Get-VM | Get-View -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") | Select -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}},  @{N="Running OS";E={$_.Guest.GuestFullName}} | Format-Table -AutoSize

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean 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 = "Folder"; E = {$_.Folder.Name}}


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Thank you.

But I also need the Provisioned Space and the used space.

Could you help me out?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which of the scripts in this thread are you using?


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

this one:

Get-VM |

Select Name,

@{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}}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, just add that new calculated property.

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}}


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Thank you!

It works perfectly! I've made a mistake earlier 😉 That's why it didn't work right away

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

I would also like to sort the VMs per folder, as we are manipulating objects, how could I insert "Sort-Object -Descending" for the folders?

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos