Automation

 View Only
Expand all | Collapse all

Script to collect resources HA-cluster

  • 1.  Script to collect resources HA-cluster

    Posted Dec 16, 2010 12:28 PM

    Hello,

    I need a powercli script that's collect the following:

    Per HA-cluster:

    An overview of the ESX servers

    Per ESX server:

    An overview of the shared datastores and size, total cpu and total memory.

    How to combine the following cmdlets?

    get-cluster "cluster" | Get-VMHost | select name, CPUTotalMHz, MemoryTotalMB

    get-cluster "cluster" | get-vmhost | Get-Datastore | select name, CapacityMB



  • 2.  RE: Script to collect resources HA-cluster

    Posted Dec 16, 2010 01:05 PM

    How do you want to display this data ?

    Since a host can have more than 1 datastore I assume you want a line per datastore ?

    Does the output go to the console screen or to a file (.csv or .txt) ?



  • 3.  RE: Script to collect resources HA-cluster

    Posted Dec 16, 2010 01:17 PM

    I have made a script to combine the two outputs. I hope this is what you want:

    Get-Cluster | `
      Sort-Object -Property Name | `
      ForEach-Object { 
      $Cluster = $_
      $Cluster | `
        Get-VMHost | `
          Sort-Object -Property Name | `
          ForEach-Object {
            $VMHost = $_
            $Report = "" | `
              Select-Object -Property Cluster,VMHost,CPUTotalMHz,MemoryTotalMB,Datastore,CapacityMB
            $Report.Cluster = $Cluster.Name
            $Report.VMHost = $VMHost.Name
            $Report.CPUTotalMHz = $VMHost.CPUTotalMHz
            $Report.MemoryTotalMB = $VMHost.MemoryTotalMB
            $Report
            $VMHost | `
              Get-Datastore | `
                ForEach-Object {
                  $Datastore = $_
                  $Report = "" | `
                    Select-Object -Property Cluster,VMHost,CPUTotalMHz,MemoryTotalMB,Datastore,CapacityMB
                  $Report.Cluster = $Cluster.Name
                  $Report.VMHost = $VMHost.Name
                  $Report.Datastore = $Datastore.Name
                  $Report.CapacityMB = $Datastore.CapacityMB
                  $Report
                }
          }
    } | Format-Table -AutoSize
    
    
    

    Regards, Robert



  • 4.  RE: Script to collect resources HA-cluster

    Posted Dec 16, 2010 01:38 PM

    Hello Robert,

    The format-table generates the following error:

    out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not
    in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with th
    e default formatting.
        + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperationException
        + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

    For the rest the  script is working. Any idea what's going wrong with the format-table?

    Vincent



  • 5.  RE: Script to collect resources HA-cluster

    Posted Dec 16, 2010 02:04 PM

    Hi Vincent,

    the script is working fine in my environment, except that I saw that the datastore names were not sorted. I made a new version to correct that.

    I have an idea what is going wrong with the Format-Table cmdlet. You get this error if you want to mix two kind of records and pipe them to Format-Table. If you run the script without the Format-Table cmdlet do you see records that have a different format than the others? That might be the problem. If you see them can you give me an example of the different records?

    Here is the new version of the script with the datastore names sorted:

    Get-Cluster | `
      Sort-Object -Property Name | `
      ForEach-Object { 
        $Cluster = $_
        $Cluster | `
          Get-VMHost | `
          Sort-Object -Property Name | `
          ForEach-Object {
            $VMHost = $_
            $Report = "" | `
              Select-Object -Property Cluster,VMHost,CPUTotalMHz,MemoryTotalMB,Datastore,CapacityMB
            $Report.Cluster = $Cluster.Name
            $Report.VMHost = $VMHost.Name
            $Report.CPUTotalMHz = $VMHost.CPUTotalMHz
            $Report.MemoryTotalMB = $VMHost.MemoryTotalMB
            $Report
            $VMHost | `
              Get-Datastore | `
              Sort-Object -Property Name | `
              ForEach-Object {
                $Datastore = $_
                $Report = "" | `
                  Select-Object -Property Cluster,VMHost,CPUTotalMHz,MemoryTotalMB,Datastore,CapacityMB
                $Report.Cluster = $Cluster.Name
                $Report.VMHost = $VMHost.Name
                $Report.Datastore = $Datastore.Name
                $Report.CapacityMB = $Datastore.CapacityMB
                $Report
              }
          }
    } | Format-Table -AutoSize
    
    

    Regards, Robert



  • 6.  RE: Script to collect resources HA-cluster

    Posted Dec 16, 2010 02:15 PM

    Hello Robert,

    What version of PowerCLI are you Using? I'm using PowerCLI 4.1.1

    Here an example of the output when I'm marked out the Format-Table cmdlet:

    Cluster          : cluster1
    VMHost        : esx1.server
    CPUTotalMHz   : 4096
    MemoryTotalMB : 64000
    Datastore     :
    CapacityMB    :


    Cluster       : cluster1
    VMHost        : esx1.server
    CPUTotalMHz   :
    MemoryTotalMB :
    Datastore     : lun1
    CapacityMB    : 20000

    Vincent



  • 7.  RE: Script to collect resources HA-cluster

    Posted Dec 16, 2010 02:44 PM

    Hi Vincent,

    that is what I intended and it works for me. I changed the script to have the CPUTotalMhz and MemoryTotalMB properties filled for every datastore. So you now only get one type of record. Hope this will work for you with the Format-Table cmdlet.

    Get-Cluster | `
      Sort-Object -Property Name | `
      ForEach-Object { 
        $Cluster = $_
        $Cluster | `
          Get-VMHost | `
          Sort-Object -Property Name | `
          ForEach-Object {
            $VMHost = $_
            $VMHost | `
              Get-Datastore | `
              Sort-Object -Property Name | `
              ForEach-Object {
                $Datastore = $_
                $Report = "" | `
                  Select-Object -Property Cluster,VMHost,CPUTotalMHz,MemoryTotalMB,Datastore,CapacityMB
                $Report.Cluster = $Cluster.Name
                $Report.VMHost = $VMHost.Name
                $Report.CPUTotalMHz = $VMHost.CPUTotalMHz
                $Report.MemoryTotalMB = $VMHost.MemoryTotalMB
                $Report.Datastore = $Datastore.Name
                $Report.CapacityMB = $Datastore.CapacityMB
                $Report
              }
          }
    } | Format-Table -AutoSize
    
    



  • 8.  RE: Script to collect resources HA-cluster

    Posted Dec 17, 2010 07:49 AM

    Hello Robert,

    I'm trying to find why the "format-table" cmdlet isn't working correctly here.  :smileyconfused:

    Vincent



  • 9.  RE: Script to collect resources HA-cluster

    Posted Dec 17, 2010 08:12 AM

    Robert,

    Got it working!

    Format-Table -AutoSize | out-default

    Found an old article about a bug.

    https://connect.microsoft.com/PowerShell/feedback/details/152205/bug-with-default-formatter

    Vincent



  • 10.  RE: Script to collect resources HA-cluster
    Best Answer

    Posted Dec 18, 2010 04:18 PM

    Hi Vincent,

    I'm glad you fixed it. And thanks for the link. I didn't know this trick.

    Regards, Robert



  • 11.  RE: Script to collect resources HA-cluster

    Posted Dec 20, 2010 07:07 AM

    Hello Robert,

    When I'm running the script you posted from the PowerCLI commandline or I right-click the script and select "run with powershell" the script works correct. When I run the script out of the Powershell editor or the PowerGui Script editor without "out-default" I receive the error as earlier posted.

    So it looks like that this problem only appear when running the script out of a script editor.

    Vincent



  • 12.  RE: Script to collect resources HA-cluster

    Posted Dec 20, 2010 09:28 AM

    Hi Vincent,

    I have tested the script only from the PowerCLI window. But I have made the window wider than the default. Maybe that makes the difference.

    Regards, Robert



  • 13.  RE: Script to collect resources HA-cluster

    Posted Dec 20, 2010 09:32 AM

    Hello Robert,

    Even in a default PowerCLI window the script is working normal. So I think we can stop this discussion because the script works fine. Thanks a lot!

    Vincent