Btw, I'm wondering why the Where clause is there, it is not filtering out any ESXi hosts afaik.
Perhaps you should also consider using the Filter parameter on the Get-View cmdlet ?
Something like this
$username = 'Administrator'
$password = 'password'
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Connect-VIServer -Server vCenter01 -Credential $cred
$now = Get-Date
Get-View -ViewType "HostSystem" -property Name, Summary -Filter @{"Name"="MyEsx*"} |
Select Name, @{N="Uptime"; E={$now - $_.Summary.Runtime.BootTime} |
Sort Name | Export-csv C:\HostsUpTime.csv
Disconnect-VIServer -confirm:$false
Send-MailMessage -From vSphere@Domain.com -To helpdesk@Domain.com -Subject "vSphere Hosts UpTime" -SmtpServer SMTPHUB001 -Attachments C:\Scripts\HostsUpTime.csv
If you want all the ESXi hosts, leave out the Filter parameter.