VMware Cloud Community
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Script to export VMs from vCenter - including Folders / Folder path

Is there a way/script to export all VMs from a vCenter with "all common" data as, name, state, space, vcpu, mem, uptime etc...AND which folder the VM resides in?
If I (in GUI) manually export data in the VMs view, I can select all these parameters, but NOT the "Folder" option.


I need a list (csv?) of all VMs with this info:

Name - State - Status - Host - Cluster - Provisioned Space - Guest OS - Memory Size - CPUs - Uptime - IP Address - DNS Name AND Folder

This is so I can sort out and sum up resources for all VMs in for example the "Development Team" folder

Any bright ideas?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

 

Get-VM | 
select Name, @{N="FolderName";E={ $_.Folder.Name}},
    @{N='FolderPath';E={
        $path = $_.Name

        $parent = Get-View -Id $_.ExtensionData.Parent
        while($parent){
            $path = "$($parent.Name)\$path"
            if($parent.Parent){
                $parent = Get-View -Id $parent.Parent
            }
            else{
                $parent = $null
            }
        }
        $path}},
    NumCpu, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, PowerState, OS, VMHost, 
    @{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}}, 
    @{N="IPAddress";E={$_.Guest.IPAddress[0]}}, 
    @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}} |
Export-Csv C:\Temp\Result2.csv -NoTypeInformation

 


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

View solution in original post

21 Replies
LucD
Leadership
Leadership
Jump to solution

Did you search this community?


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Yes, but I see that I left something out in my question;

Regarding the folder, I want to output all VMs from the "top" folder.

As in

-Cuscomer1
    -test
    -dev

-Customer2
    -test
    -dev

-Customer3
     -test
     -dev

I want the "folder" column in the output csv to list if the VMs is located in the folder Customer1, 2 or 3 (not test or dev)

 

If I use this;
I only get the direct folder the VM is located under (test or dev)

$VMs = Get-VM
$Output = foreach ($VM in $VMs){
Get-VM $VM | select Name, @{N="FolderName";E={ $_.Folder.Name}}, NumCpu, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, PowerState, OS, VMHost, @{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}}, @{N="IPAddress";E={$_.Guest.IPAddress[0]}}, @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}}
}
$Output | Export-Csv C:\Temp\Result2.csv -NoTypeInformation

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You might have a look at the function in Get the folderpath
With the full path you can check for Customer1, Customer2, ...


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Thanks.

Do you know if there is a way to "bake" that into my script, so the folderpath for each VM will show as a column (as all the other values) in the output csv file?

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use the Get-FolderPath function in a calculated property.
This assumes you do not have folders with the same name in different locations in your inventory.


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Sorry...not the most experienced scripter here...

Can you give an example?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

 

Get-VM | 
select Name, @{N="FolderName";E={ $_.Folder.Name}},
    @{N='FolderPath';E={
        $path = $_.Name

        $parent = Get-View -Id $_.ExtensionData.Parent
        while($parent){
            $path = "$($parent.Name)\$path"
            if($parent.Parent){
                $parent = Get-View -Id $parent.Parent
            }
            else{
                $parent = $null
            }
        }
        $path}},
    NumCpu, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, PowerState, OS, VMHost, 
    @{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}}, 
    @{N="IPAddress";E={$_.Guest.IPAddress[0]}}, 
    @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}} |
Export-Csv C:\Temp\Result2.csv -NoTypeInformation

 


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

Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Almost....but not quite.
Now it gives me "Customer1-nono" on all VMs. (only one VM are located in that folder)

Name - FolderName - FolderPath

vCenterProdDatacenters\Nilzen\vm\Customer1\Customer1-nono\vCenter
NilzenRDSProdDatacenters\Nilzen\vm\Customer1\Customer1-nono\NilzenRDS
DownloadProdDatacenters\Nilzen\vm\Customer1\Customer1-nono\Download
SMTPProdDatacenters\Nilzen\vm\Customer1\Customer1-nono\SMTP
Customer1VMCustomer1-nonoDatacenters\Nilzen\vm\Customer1\Customer1-nono\Customer1VM
DC02DCsDatacenters\Nilzen\vm\Customer1\Customer1-nono\DC02
DC01DCsDatacenters\Nilzen\vm\Customer1\Customer1-nono\DC01
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, a typo.
I corrected the code above


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Now it works like a charm!

Thanks a lot for your excellent help, and have a great weekend 🙂

 

Tags (1)
Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Hi.

Is there a way to alter your script to give me an output that don't add the VM name?

Right now it gives:

Name - FolderName - FolderPath

PAL_OracleFolder0Datacenters\DC1\vm\Customer1\Folder0\PAL_Oracle
HUSPRO01Folder1Datacenters\DC1\vm\Customer1\Folder1\HUSPRO01
HEIPMFPRO01Folder1Datacenters\DC1\vm\Customer1\Folder1\HEIPMFPRO01

 

I need the folderpath to view "Datacenters\DC1\vm\Customer1\Folder0"  (not include the hostname)

Or even better; just dispay the "Customer1" value?

The script I'm using now:

Get-VM |
select Name, @{N="FolderName";E={ $_.Folder.Name}},
@{N='FolderPath';E={
$path = $_.Name

$parent = Get-View -Id $_.ExtensionData.Parent
while($parent){
$path = "$($parent.Name)\$path"
if($parent.Parent){
$parent = Get-View -Id $parent.Parent
}
else{
$parent = $null
}
}
$path}} |
Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformation

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this?

Get-VM |
  select Name, @{N = "FolderName"; E = { $_.Folder.Name } },
  @{N = 'Folder'; E = {
  
      $parent = Get-View -Id $_.ExtensionData.Parent
      while ($parent) {
        if ($parent.Name -match "^Customer") {
          $parent.Name
        }
        if ($parent.Parent) {
          $parent = Get-View -Id $parent.Parent
        } else {
          $parent = $null
        }
      }
    }
  } |
  Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformationAp


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Not quite;

The "Customer1" name are dynamic.

I have a lot of folders in vCenter, like

Customer1name
-test
-dev

Customer2
-mgmt
-user

Customer3
-abc
-def


I need the folderpath to show the SE00...., SE02...., SE03....

Or at least stop with that (not include hostname in the path)

Fnilsen80_0-1655992012236.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Without the VM name

Get-VM |
  select Name, @{N = "FolderName"; E = { $_.Folder.Name } },
  @{N = 'FolderPath'; E = {
      $path = ''

      $parent = Get-View -Id $_.ExtensionData.Parent
      while ($parent) {
        if($path -ne ''){
          $path = "$($parent.Name)\$path"
        }
        else{
          $path = $parent.Name
        }
        if ($parent.Parent) {
          $parent = Get-View -Id $parent.Parent
        } else {
          $parent = $null
        }
      }
      $path }
  } |
  Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformationAp


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

That looks good.

One final (I hope) question;

Is it possible to remove the last part in the folderpath?

So that is stops with the customername (SE02....)

Like:

Datacenters\Sto1\vm\SE00-PSCloud_SE\

Instead of

Datacenters\Sto1\vm\SE00-PSCloud_SE\MGMT

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

 

Get-VM |
  select Name, @{N = "FolderName"; E = { $_.Folder.Name } },
  @{N = 'FolderPath'; E = {
      $path = ''

      $parent = Get-View -Id $_.ExtensionData.Parent
      while ($parent) {
        if($path -ne ''){
          $path = "$($parent.Name)\$path"
        }
        else{
          $path = $parent.Name
        }
        if ($parent.Parent) {
          $parent = Get-View -Id $parent.Parent
        } else {
          $parent = $null
        }
      }
      Split-Path -Path $path }
  } |
  Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformationAp

 


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Not quite...now the folderpath is blank>

Fnilsen80_0-1656012949854.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I had a typo in the Split-Path cmdlet.
I corrected the code above


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

Reply
0 Kudos
Fnilsen80
Enthusiast
Enthusiast
Jump to solution

Hi.

The VM name is gone, but it still reports folders after the SE00-xyz\

Fnilsen80_0-1656056825916.png

 

Reply
0 Kudos