0

I don´t know how I can create a log file for my script in order to know if the files was created succesfully and then if e-mail was sent.

My script generates a report and then sends it over e-mail.

This is the command that generates my HTML report and send e-mail.

Import-Csv -Delimiter "," "E:\SAM\INFORMES\PRINTERS\Informe_de_consumo_PRINTER1 $(Get-Date -f dd-MM-yyyy)Completo3.csv"
ConvertTo-Html -Head $css -Body "<h1>Informes de consumo para la PRINTER1 </h1>`n<h5>Generado el $(Get-Date -f dd-MM-yyyy) por la SAM</h5>"
Out-File "E:\SAM\INFORMES\PRINTERS\Informe_de_consumo_PRINTER 1 $(Get-Date -f dd-MM-yyyy).html" -Encoding utf8

# This is the command to send e-mail

##example:
$From = "informesconsumo@xxx.es"
$To = "xxxxxx@xxxx.es"
$Bcc = "xxxxxxxxxxx@xxxxx.com","xxxxx@xxx.es"
$Attachment = "E:\SAM\INFORMES\PRINTERS\Informe_de_consumo_PRINTER1 $(Get-Date -f dd-MM-yyyy).csv","E:\SAM\INFORMES\PRINTERS\Informe_de_consumo_PRINTER1 $(Get-Date -f dd-MM-yyyy).html"
$Subject = "Informe de consumo PRINTER 1"
$body = "<font face=arial color=black>This is the report for the PRINTER1</font><br>
$SMTPServer = "smtp.xxxxx.es"
$SMTPPort = "xx"
Send-MailMessage -From $From -To $To -Bcc $Bcc -Subject $Subject -Body $Body -BodyAsHtml -Encoding utf8 -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Attachments $Attachment

How can I put a log in order to see at the end of the day that the file generated properly and was sended.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Mr Printer
  • 29
  • 6
  • 3
    You're looking for [`Start-Transcript`](https://stackoverflow.com/a/23010372/1630171). – Ansgar Wiechers Jul 02 '18 at 09:55
  • 1
    As a side note: the code you posted is broken. The first 3 commands need to be connected with pipes, and the line `$body = "...` is missing a closing double quote. – Ansgar Wiechers Jul 02 '18 at 10:01
  • Yes , i know, i deleted because , the stackoverflow, gave me an error about 4 spaces or something like that. And the body was modified. That is not the original text so i forgot the " Thank you. – Mr Printer Jul 02 '18 at 11:24
  • I used start-transcript, but it´s not giving me information about if the file was created or no. It´s just saying the start and the end hour, and my user. – Mr Printer Jul 02 '18 at 11:25
  • It should show whatever error is thrown when the file could not be created. If that is not sufficient you'll need to add checks to test if the file actually has been created (and has the desired content). – Ansgar Wiechers Jul 02 '18 at 11:29
  • Could you please show me , how to add checks? – Mr Printer Jul 02 '18 at 11:30
  • Use [`Test-Path`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-3.0) to check the existence of a file. Content checks can get as intricate as you like, so I'm going to leave that to you. – Ansgar Wiechers Jul 02 '18 at 11:32
  • I need some examples. Transcript it´s not working as i ask. I need to see if the file was created, and if the e-mail was sended – Mr Printer Jul 02 '18 at 11:44
  • The `Test-Path` documentation has examples. `Send-MailMessage` will throw an error if the command fails, otherwise the mail was successfully sent. – Ansgar Wiechers Jul 02 '18 at 13:00

1 Answers1

0

The answers given was not helpfull. I found in the Powershell documentation , that the PS Logging Module is doing what i want.

Start Transcript it´s not working as expected at least in my case.

Thank you.

Have a nice day.

Mr Printer
  • 29
  • 6