Highlighted
Expert
Expert

Host usage report

Jump to solution

Hi All,

I'm trying to generate the report for ESXi Host, CPU and Memory usage and send over mail after exporting into csv, but it fails with an error. Looking for help to fix the error.

PS D:\scripts> .\Host-report.ps1

At D:\scripts\Host-report.ps1:16 char:55

+     foreach ($cluster in Get-Cluster -Server $vcenter) }

+                                                       ~

Missing statement body in foreach loop.

At D:\scripts\Host-report.ps1:42 char:5

+     }

+     ~

Unexpected token '}' in expression or statement.

At D:\scripts\Host-report.ps1:46 char:1

+ }

+ ~

Unexpected token '}' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParseException

    + FullyQualifiedErrorId : MissingForeachStatement

$userbane = "admin"

$encrypted = Get-Content D:\Scripts\scriptsencrypted_paswd_admin.txt | ConvertTo-SecureString

$Cred = New-Object System.Management.Automation.PsCredential($userbane, $encrypted)

$vCenters = (Get-Content "C:\Temp\VC.txt")

foreach ($vcenter in $vcenters) {

    Connect-VIServer $vcenter -Credential $Cred

    foreach ($cluster in (Get-Cluster -Server $vcenter)){

$report += Get-VMHost  |

Select Name,

    @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

    @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}}

    @{N='datastore Capacity GB';E={[math]::Round($_.StorageUsageGB,2)}},

    @{N='datastore Used GB';E={[math]::Round($_.StorageUsageGB,2)}},

    @{N='datastore Free GB';E={[math]::Round(($_.StorageUsageGB - $_.MemoryUsageGB),2)}}

    }

    Disconnect-VIServer -Server $vcenter -Confirm:$false

}

$report | Export-Csv -path c:\Temp\HostReport.csv -NoTypeInformation -UseCulture

$sMail = @{

    From                       = 'domain.com'

    To                         = 'domain.com'

    Subject                    = 'Remote Site Host Report'

    Body                       = "Report. Sending now."

    Attachments                = 'C:\Temp\HostReport.csv'

    Priority                   = 'High'

    DeliveryNotificationOption = 'OnSuccess', 'OnFailure'

    SmtpServer                 = 'smtp.domain.com'

}

Send-MailMessage @sMail

Thanks

V

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

You are not storing anything in $report.

Change the Get-VMHost line to

$report += Get-VMHost  |


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

View solution in original post

0 Kudos
17 Replies
Highlighted
User Moderator
User Moderator

Change that line to the following.
PS: I don't understand why you have curly braces around that foreach in the error message

foreach ($cluster in (Get-Cluster -Server $vcenter)){

}


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

0 Kudos
Highlighted
Expert
Expert

LucD,

I have corrected the above code, but it generates blank report in csv, while in power cli console it shows output.

pastedImage_0.png

Thanks

V

0 Kudos
Highlighted
User Moderator
User Moderator

You are not storing anything in $report.

Change the Get-VMHost line to

$report += Get-VMHost  |


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

View solution in original post

0 Kudos
Highlighted
Expert
Expert

Thank you, LucD

Yes, it worked this time, but Data store capacity not generating in the report. Still any thing to correct ?

Thanks

V

0 Kudos
Highlighted
User Moderator
User Moderator

Those Storage* properties do not exist on a VMHost object.

Where did you get that code from?


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

0 Kudos
Highlighted
Expert
Expert

LucD,

It's my mistake, i got the code from your old thread. Host usage.

Thanks

V

0 Kudos
Highlighted
Expert
Expert

LucD,

is possible to get cluster name in the above report as well ? Please !!

Also, i observed that host name are getting repeated. is it due to vCenter not getting disconnected ? I'm running this script on a daily basis from the Tasks scheduler.

Let me know if you want me to open a new thread. Sorry for checking this issue late.

Cluster NameHost NameCPU GHz CapacityCPU GHz UsedCPU GHz FreeMemory Capacity GBMemory Used GBMemory Free GB

Thanks

V

0 Kudos
Highlighted
User Moderator
User Moderator

Try like this

$userbane = "admin"

$encrypted = Get-Content D:\Scripts\scriptsencrypted_paswd_admin.txt | ConvertTo-SecureString

$Cred = New-Object System.Management.Automation.PsCredential($userbane, $encrypted)

  

$vCenters = (Get-Content "C:\Temp\VC.txt")


$report = @()


foreach ($vcenter in $vcenters) {

   Connect-VIServer $vcenter -Credential $Cred


   $report += Get-Cluster -Server $vcenter -PipelineVariable cluster |

      Get-VMHost |

      Select-Object Name,

      @{N = 'Cluster'; E = { $cluster.Name } },

      @{N = 'CPU GHz Capacity'; E = { [math]::Round($_.CpuTotalMhz/1000, 2) } },

      @{N = 'CPU GHz Used'; E = { [math]::Round($_.CpuUsageMhz/1000, 2) } },

      @{N = 'CPU GHz Free'; E = { [math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000, 2) } },

      @{N = 'Memory Capacity GB'; E = { [math]::Round($_.MemoryTotalGB, 2) } },

      @{N = 'Memory Used GB'; E = { [math]::Round($_.MemoryUsageGB, 2) } },

      @{N = 'Memory Free GB'; E = { [math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB), 2) } },

      @{N = 'datastore Capacity GB'; E = { [math]::Round($_.StorageUsageGB, 2) } },

      @{N = 'datastore Used GB'; E = { [math]::Round($_.StorageUsageGB, 2) } },

      @{N = 'datastore Free GB'; E = { [math]::Round(($_.StorageUsageGB - $_.MemoryUsageGB), 2) } }

 

   Disconnect-VIServer -Server $vcenter -Confirm:$false

}


$report | Export-Csv -path c:\Temp\HostReport.csv -NoTypeInformation -UseCulture


$sMail = @{

   From                       = 'domain.com'

   To                         = 'domain.com'

   Subject                    = 'Remote Site Host Report'

   Body                       = "Report. Sending now."

   Attachments                = 'C:\Temp\HostReport.csv'

   Priority                   = 'High'

   DeliveryNotificationOption = 'OnSuccess', 'OnFailure'

   SmtpServer                 = 'smtp.domain.com'

}

Send-MailMessage @sMail


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

0 Kudos
Highlighted
Expert
Expert

LucD,

I ran the script, but each host reporting twice with same value and the Cluster name is missing. Any idea why ? Overall count of host making 2100 which is quite surprising, though we have 100 hosts roughly. is it power Cli array value adding continuously or repetitive adding ?

pastedImage_0.png

Thanks

V

0 Kudos
Highlighted
User Moderator
User Moderator

It looks as if you have not copied my last code correctly.
Can you attach (as a file) the code you are currently running?


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

0 Kudos
Highlighted
Expert
Expert

Here you go.  PFA

Thanks

V

0 Kudos
Highlighted
User Moderator
User Moderator

The initialisation of the $report array was missing.

I corrected the code above


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

0 Kudos
Highlighted
Expert
Expert

LucD,

It's weird, i tried multiple times copying exact code to rule out copy paste error, but still report generates 2144 host ( repeating) though we have 298 hosts in 10 vCenter.

Thanks

0 Kudos
Highlighted
User Moderator
User Moderator

The attached script works for me.


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

0 Kudos
Highlighted
Expert
Expert

It's fixed now. When i ran the code from the different workstation, it is working. Smiley Happy

It was due to power Cli, i guess.

0 Kudos
Highlighted
Expert
Expert

LucD,

Does your report shows Data store as  Capacity as 0 GB /Used 0 GB and Data store Free GB as negative ? Any idea why it shows as negative and 0 value for all the datastore  ?

   

datastore Capacity GBdatastore Used GBdatastore Free GB
00-39.81
00-16.45
00-39.5
00-4.2
00-4.27
00-44.12
00-59.31
00-68.92

Thanks

V

0 Kudos
Highlighted
User Moderator
User Moderator

Because the properties you are using do not exist on a VMHost object.


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

0 Kudos