VMware Communities > Developer Community > VI Toolkit (for Windows) > Discussions

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
3 Replies Last post: Nov 23, 2008 11:30 AM by PeterVG
Reply

Error Trapping / Creating and Emailing Logs

Oct 2, 2008 5:44 AM

Click to view tonygent's profile Novice tonygent 22 posts since
May 30, 2006

Hi All,

With this 'BIG' script I'm writing - I'd like to be able to schedule the script to run, log the output to a file and then email the file to the Admin at the end.

At the moment I've been able to run the script manually - sending data out of the Std-out with 'write-host'. However - how am I best to write to a log file??

Can I just write-host "test test" > logfile.log?? - if so - how can I append consistently to this log.

And Once I have it - how can I grab the contents in as a string to send as email??

I've done elements like this in the past with VB6 (I know, I know - thats just how old I am!). Creating a log object - init-ing the object creates the log, sending a method of "Add Entry" with the Text to add and the Object then writes with the current date/time followed by the string etc. Is this sort of Obj Orientated code possible here?? Would be nice and easy to port the Logging object/functions between scripts if called as a remote function (see answers from Hal and Luc in my previous post).

Thanks, as always, in anticipation.

TG

Reply Re: Error Trapping / Creating and Emailing Logs Oct 2, 2008 7:00 AM
Click to view LucD's profile Master LucD 858 posts since
Oct 31, 2005
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)
Reply Re: Error Trapping / Creating and Emailing Logs Nov 23, 2008 11:01 AM
in response to: LucD
Click to view PeterVG's profile Novice PeterVG 9 posts since
Nov 29, 2004

Hi,

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 ?

BR, Peter

Reply Re: Error Trapping / Creating and Emailing Logs Nov 23, 2008 11:30 AM
in response to: PeterVG
Click to view PeterVG's profile Novice PeterVG 9 posts since
Nov 29, 2004

All,

never mind, forgot to remove the >< signs...
All is working now.

Peter

Actions