VMware Cloud Community
vrm
Contributor
Contributor
Jump to solution

Script to collect resources HA-cluster

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

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Vincent,

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

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

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) ?


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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
vrm
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
vrm
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
vrm
Contributor
Contributor
Jump to solution

Hello Robert,

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

Vincent

Reply
0 Kudos
vrm
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Vincent,

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

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
vrm
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
vrm
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos