Hmm, this is what I have:
clear-host
$vcserver = "vcenter"
$mailto = "Me"
$SMTPServer = "exchange"
$msg = new-object Net.Mail.MailMessage
Function Report-Datastore {
$output = @()
Get-Datastore | % {
$props = [ordered]@{'Name'=$_.Name;
'UsedSpace'=[math]::Round(($_.CapacityGB - $_.FreeSpaceGB),2);
'PercFree'=[math]::Round((100 * ($_.FreeSpaceGB/$_.CapacityGB)),0)}
$output += New-Object -TypeName PSCUstomObject -Property $props
}
$output
}
$msg.Subject = "vCenter Datastore Health CompanyvCenter"
Connect-VIServer $vcserver
$html_DS = Report-Datastore |
Sort-Object PercFree |
ConvertTo-HTML -Fragment |
Out-String
$html_DS = "<h2>Datastores</h2>$html_DS"
$params = @{'Head'="<title>vCenter Datastore Health CompanyvCenter</title>";
'PreContent'="<h1>Datastore HealthCheck CompanyvCenter</h1>";
'PostContent'=$html_DS}
$msg.Body = ConvertTo-HTML @params
#$msg.Attachments.Add($att1)
$msg.IsBodyHTML = $true
$smtp.Send($msg)
#$att1.Dispose()
Disconnect-VIServer $vcserver -Confirm:$false
Got the following error:
You cannot call a method on a null-valued expression.
At C:\scripts\VMware\DatastoreCheck.ps1:31 char:1
+ $smtp.Send($msg)
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull