Automation

 View Only
  • 1.  Use Get-Folder to get a count of all VMs in folder structure.

    Posted Jun 26, 2011 03:18 AM

    Hi All,

    Tried some searching in the community, but couldn't find exactly what I was looking for.  We have a folder structure setup in vCenter.  We have some companies we have bought over time and we setup a seperate folder for them with a top level folder and nested folders for different VM types below, something like:

    CompanyX

         - Email Servers

         - Web Servers

         - DB Servers

    For chargeback purposes, I have been asked to get a count of all the VMs for each company.  I can get the count for the VMs directly in the "CompanyX" folder, but it doesn't include the VMs in the sub folders.  This is the script I have used so far:

    Write-Output "Total VM Count for CompanyX VMware Environment: $((Get-Folder 'CompanyX' | Get-VM).count)" | out-file "D:\ScriptResults\CompanyX.txt" -append

    Like I said, that works fine for the VMs in right in the parent folder, but it doesn't go down the next layer.  Is there a way to make get-folder do that?

    I am PowerShell/PowerCLI novice, so forgive me if I am missing something obvious.

    Thanks,

    Bob



  • 2.  RE: Use Get-Folder to get a count of all VMs in folder structure.
    Best Answer

    Posted Jun 26, 2011 06:44 AM

    Hi Bob,

    I don't know if this was different in previous versions but in the latest PowerCLI version 4.1 U1 if you do

    Get-Folder 'CompanyX' | Get-VM

    it will retrieve the VM's in the top folder and all the subfolders. You can easily check this with:

    Get-Folder 'CompanyX' | Get-VM | Select-Object -Property Name,Folder

    If you only want the VM's in the top folder you can use:

    Get-Folder 'CompanyX' -NoRecursion | Get-VM


    The next script will get the VM count for all companies and write the result in a .csv file.

    $ExcludeFolders = "Discovered virtual machine","other folder to exclude"
    Get-Folder vm | Get-Folder -NoRecursion | `
    Where-Object {$ExcludeFolders -notcontains $_.Name } | `
    ForEach-Object {
      $Folder = $_
      $Report = "" | Select-Object -Property Company,"Number of VM's"
      $Report.Company = $Folder.Name
      $Report."Number of VM's" = ($Folder | Get-VM | Measure-Object).Count
      $Report
    } | Export-Csv -Path CompanyVMsCount.csv -NoTypeInformation -UseCulture 
    
    

    If there are top folders that you want to exclude from the list you can add them to the $ExcludeFolders variable in the first line off the script. The second line of the script gets all the "vm" top folders. The third line filters the unwanted folders. The next few lines create the output objects. And the last line writes the objects to a .csv file.

    I added the Measure-Object cmdlet to the ($Folder | Get-VM | Measure-Object).Count because otherwise you will get an error if the count is 0 or 1.

    Regards, Robert



  • 3.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted Jun 26, 2011 10:51 AM

    Afaik the Get-VM cmdlet has always been recursive by default.

    That is why you have the NoRecursion switch to change this default behaviour.

    There must be something else that goes wrong. Which PowerCLI build are you using ?

    Get-PowerCLIVersion



  • 4.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted Jun 27, 2011 02:29 PM

    Robert/LucD,

    Thanks for your responses.  I am feeling a little stupid as you are correct, it is pulling the right number, the problem was with the way I was verifying the number of VMs it was coming back with was no correct.  I was looking in vCenter and clicking the top level folder (CompanyX) and counting the number of VMs displayed in the right window pane, assuming these were the VMs in the CompanyX folder, when, in fact, that is the recursive view and shows all VMs in the tree below.  There aren't actually any VMs in the top level folder, if there were, they'd be displayed on the left window pane in vCenter, under the folder structure.   So, just me not understanding the views and looking closely enough at the actual list of VMs being brought back.  Sorry about that.

    On side note, the command that Robert provided:

    Get-Folder 'CompanyX' | Get-VM | Select-Object -Property Name,Folder

    Returns the VM name, but the folder column is empty for some reason.  Also, if I run:

    Get-Folder 'Company X' -NoRecursion | Get-VM

    I get an error:

    Get-Folder : 6/27/2011 9:17:50 AM    Get-Folder        Folder with name 'Company X' not found, using the specified filter(s).
    At line:1 char:11
    + Get-Folder  <<<< 'Company X' -NoRecursion | Get-VM

    I moved a VM directly into the 'Company X' folder and got the same results.

    Not a big deal, just curious as to why they don't work.

    Thanks for both of your help!

    Bob



  • 5.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted Jun 27, 2011 05:38 PM

    It looks as if you might be using an older build of PowerCLI.

    What does this say

    Get-PowerCLIVersion


  • 6.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted Jun 27, 2011 06:03 PM

    PowerCLI Version
    ----------------
    VMware vSphere PowerCLI 4.0 U1 build 208462

    We have not upgraded to vSphere 4.1 yet, so I haven't updated powercli either.  Can I use powercli 4.1 against a 4.0 vSphere environment?

    Thanks,

    Bob



  • 7.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted Jun 27, 2011 06:20 PM

    If you have at least 4.0 U2 then you can use PowerCLI 4.1U1.

    See the Release Notes.



  • 8.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted May 16, 2014 05:15 PM

    Hopefully someone would be able to help me with that. I am a novice as well to PowerShell/PowerCli, and can understand mostly the answer(s) from above. Which are great. Helps immensely with what I'm doing. I do have a question though, how can I specify a specific folder/folders and retrieve only that folder's vm count?

    In our environment, we have 50+ folders, and I'm tasked with monitoring say -- 12 of them. I've attempted to modify the script to read a Get-Content text file that has a listing of the folders I'd like, but can't seem to get for example:

    Name                    Count

    ---------                    ---------

    Pool1                       10

    Pool2                       15

    .

    .

    Pool12                     12

    Tried multiple filters, and have resorted to simply asking if anyone can give me some guidance or assist in any way -- I'd be most appreciative.



  • 9.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted May 16, 2014 05:25 PM

    Try like this (it assumes you have each of the folders to monitor on a line in the TXT file

    Get-Content -Path folders.txt | %{
     
    New-Object PSObject -Property @{
       
    Folder = $_
       
    VMCount = Get-Folder -Name $_ | Get-VM | Measure-Object | Select -ExpandProperty Count
      }
    }


  • 10.  RE: Use Get-Folder to get a count of all VMs in folder structure.

    Posted May 16, 2014 05:37 PM

    Simply amazing. Thank you LucD for your response. I'd tried your instructions and the results were exactly what I'd hoped for. Thank you again for your assistance. :smileyhappy: