Automation

 View Only
Expand all | Collapse all

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

LucD

LucDJun 09, 2022 01:31 PM

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

    Posted Jun 09, 2022 11:52 AM

    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?



  • 2.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 09, 2022 01:31 PM

    Did you search this community?



  • 3.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 09, 2022 03:16 PM

    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



  • 4.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 09, 2022 04:46 PM

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



  • 5.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 10, 2022 06:22 AM

    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?

     



  • 6.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 10, 2022 08:17 AM

    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.



  • 7.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 10, 2022 03:34 PM

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

    Can you give an example?



  • 8.  RE: Script to export VMs from vCenter - including Folders / Folder path
    Best Answer

    Posted Jun 10, 2022 04:54 PM

    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

     



  • 9.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 10, 2022 05:10 PM

    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


  • 10.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 10, 2022 05:16 PM

    My bad, a typo.
    I corrected the code above



  • 11.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 10, 2022 08:58 PM

    Now it works like a charm!

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

     



  • 12.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 01:24 PM

    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



  • 13.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 01:31 PM

    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


  • 14.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 01:49 PM

    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

     



  • 15.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 01:53 PM

    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


  • 16.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 02:00 PM

    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



  • 17.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 03:41 PM

    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

     



  • 18.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 07:35 PM

    Not quite...now the folderpath is blank>

    Fnilsen80_0-1656012949854.png

     



  • 19.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 23, 2022 07:45 PM

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



  • 20.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 24, 2022 07:47 AM

    Hi.

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

    Fnilsen80_0-1656056825916.png

     



  • 21.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 24, 2022 08:48 AM

    Strange, works for me.
    Then I'm at my wit's end I'm afraid.



  • 22.  RE: Script to export VMs from vCenter - including Folders / Folder path

    Posted Jun 24, 2022 10:41 AM

    I'll do some more testing, but I am more than helped! You're a wizard

    Thanks a lot.