For writing output to a file you best use the Write-Output cmdlet.
And you use the "classic" redirection (like in DOS) to direct it to file.
Something like this
Write-Output "Line 1" > c:\temp\report.txt Write-Output "Line 2" >> c:\temp\report.txt Write-Output "Line 3" >> c:\temp\report.txt
The first redirect initialises the output file.
If you don't want that replace ">" by ">>".
Getting the contents of the file into a variable is something that PowerShell does very well.
$report = Get-Content c:\temp\report.txt
Sending the report with email can be done like this
$SmtpClient = New-Object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.host = "<smtp-server>" $MailMessage.from = "<from-address>" $MailMessage.To.add("<to-address>") $MailMessage.IsBodyHtml = 1 $MailMessage.Subject = "My report" $MailMessage.body = "report attached" $MailMessage.Attachments.Add("c:\temp\report.txt") $SmtpClient.Send($MailMessage)
am trying to send out a mail with the exact above commands in a script, but I keep on getting an error on the last send command:
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\DMZvm.ps1:21 char:17
+ $SmtpClient.Send( <<<< $MailMessage)
Am using the exact commands like above (obviously tried some variations), but always get same error ??
Any idea ?
never mind, forgot to remove the >< signs...
All is working now.