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
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
Change that line to the following.
PS: I don't understand why you have curly braces around that foreach in the error message
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I have corrected the above code, but it generates blank report in csv, while in power cli console it shows output.
Thanks
V
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
Thank you, LucD
Yes, it worked this time, but Data store capacity not generating in the report. Still any thing to correct ?
Thanks
V
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
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 Name | Host Name | CPU GHz Capacity | CPU GHz Used | CPU GHz Free | Memory Capacity GB | Memory Used GB | Memory Free GB |
Thanks
V
Try like this
$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
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 ?
Thanks
V
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
The initialisation of the $report array was missing.
I corrected the code above
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
V
The attached script works for me.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It's fixed now. When i ran the code from the different workstation, it is working.
It was due to power Cli, i guess.
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 GB | datastore Used GB | datastore Free GB |
0 | 0 | -39.81 |
0 | 0 | -16.45 |
0 | 0 | -39.5 |
0 | 0 | -4.2 |
0 | 0 | -4.27 |
0 | 0 | -44.12 |
0 | 0 | -59.31 |
0 | 0 | -68.92 |
Thanks
V
Because the properties you are using do not exist on a VMHost object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference