VMware Cloud Community
eaalvare
Contributor
Contributor

Sending an email after running a psi script

Hello-

Before I start playing with the script I wanted to know if the script that I run will execute an email with the results or an email that the script ran successfully.

Scenerio: Currently I have a ps1 script that runs automatically daily(testing for now) that executes my vm inventory. It gathers the info and creates a csv file on my c drive. What I would like to add is the send mail feacture, which I have included in my sample script, but it does not do anything. It might be that my smtp settings are not allowing, but want to verify if this script is correct.

Here is script

================================================================================================

Connect-VIServer myVC -User  -Password
$report = foreach($vm in Get-VM){
    Get-HardDisk -VM $vm | Select @{N="VM Name";E={$vm.Name}},
    @{N="RDM Name";E={($_ | where {"RawPhysical","RawVirtual" -contains $_.DiskType}).FileName}},
    @{N="Datastore";E={($_ | where {"RawPhysical","RawVirtual" -notcontains $_.DiskType}).Filename.Split(']')[0].TrimStart('[')}},
    @{N="HD Name";E={$_.Name}},
    @{N="HD Size GB";E={$_.CapacityKB/1MB}},
    @{N="vCPU";E={$vm.NumCpu}},
    @{N="vRAM (MB)";E={$vm.MemoryMB}}
}
$report | Export-Csv "C:\report_4_.csv" -NoTypeInformation -UseCulture

$smtpSrv = "smtp.domain.com"
$from = "vmwaretimepolice@domain.com"
$to = "recieipt@domain.com"
$subject = "VM Host Time Report"
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$smtp = new-object Net.Mail.SMTPclient($smtpSrv)
$smtp.send($msg)

================================================================================================

Thanks,

Everett

0 Kudos
1 Reply
LucD
Leadership
Leadership

Your script doesn't seem to put anything in the $body variable.

That explains the empty

Try it like this

Connect-VIServer myVC -User  -Password 
$report = foreach($vm in Get-VM){     Get-HardDisk -VM $vm | Select @{N="VM Name";E={$vm.Name}},     @{N="RDM Name";E={($_ | where {"RawPhysical","RawVirtual" -contains $_.DiskType}).FileName}},     @{N="Datastore";E={($_ | where {"RawPhysical","RawVirtual" -notcontains $_.DiskType}).Filename.Split(']')[0].TrimStart('[')}},     @{N="HD Name";E={$_.Name}},     @{N="HD Size GB";E={$_.CapacityKB/1MB}},     @{N="vCPU";E={$vm.NumCpu}},     @{N="vRAM (MB)";E={$vm.MemoryMB}} } $report | Export-Csv "C:\report_4_.csv" -NoTypeInformation -UseCulture $smtpSrv = "smtp.domain.com"
$from
= "vmwaretimepolice@domain.com"
$to = "recieipt@domain.com"
$subject
= "VM Host Time Report"
$body
= $report | Out-String
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body) $smtp = new-object Net.Mail.SMTPclient($smtpSrv) $smtp.send($msg)


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

0 Kudos