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