3 Replies Latest reply on Nov 23, 2008 11:30 AM by PeterVG

    Error Trapping / Creating and Emailing Logs

    tonygent Enthusiast


      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.









        • 1. Re: Error Trapping / Creating and Emailing Logs
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          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.IsBodyHtml = 1
          $MailMessage.Subject = "My report"
          $MailMessage.body = "report attached"


          • 2. Re: Error Trapping / Creating and Emailing Logs
            PeterVG Enthusiast





            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( &lt;&lt;&lt;&lt; $MailMessage)



            Am using the exact commands like above (obviously tried some variations), but always get same error ??




            Any idea ?



            BR, Peter



            • 3. Re: Error Trapping / Creating and Emailing Logs
              PeterVG Enthusiast





              never mind, forgot to remove the &gt;&lt; signs...

              All is working now.