locked
[PS] Backup results in logfile, send as attachment RRS feed

  • Question

  • Hi All,

    I'm new to scripting and want to learn it. I have an environment with 12 servers that make a backup every day with Microsoft Windows Backup. I want to have one text file with all the results of the servers that i can email to myself.

    I made a simple script for the event ID output, i made a task that triggers on a fail or succesfull backup event id (4): 

    $text = "$(Get-Date) CC-server1: Backup completed successfully"
    $text >> 'C:\Log\Logging.txt'

    I did this for the failed task too. Also the other servers have the same script so all the results come in the same logging.txt

    Then i made a script that mails the txt file as an attachment to myself, that's not the problem.
    But how can i make a script to use the attachment (txt) as a body so i open the email and see the results in a nice body in stead of a text attachment?
    Tuesday, August 15, 2017 9:24 AM

All replies

  • Hi,

    Read the text from the file, and replace or append the body on the email with the text. 

    $body = Get-Content -path c:\log\logging.txt

    Use the $body variabel to change / append your email body.


    Best regards, Anders - MCP/CCNP

    Tuesday, August 15, 2017 9:29 AM
  • Hi,

    Read the text from the file, and replace or append the body on the email with the text. 

    $body = Get-Content -path c:\log\logging.txt

    Use the $body variabel to change / append your email body.


    Best regards, Anders - MCP/CCNP

    I made the script like this now: Send-MailMessage $body = Get-Content -path C:\log\logging.txt -SmtpServer "smtpserver" -From "backup@example.com" -To "user@example.com" -Subject "Daily Report" but i don't receive any mail, what am i doing wrong?
    Tuesday, August 15, 2017 9:55 AM
  • Hi,

    Try: 

    $body = Get-Content -path C:\log\logging.txt
    Send-MailMessage -body $body -SmtpServer "smtpserver" -From "backup@example.com" -To "user@example.com" -Subject "Daily Report"


    Best regards, Anders - MCP/CCNP

    Tuesday, August 15, 2017 9:59 AM
  • Hi,

    Try: 

    $body = Get-Content -path C:\log\logging.txt
    Send-MailMessage -body $body -SmtpServer "smtpserver" -From "backup@example.com" -To "user@example.com" -Subject "Daily Report"


    Best regards, Anders - MCP/CCNP

    Hi Anders, thank your but it doesn't work either.
    Is there any other command that will do it?
    Tuesday, August 15, 2017 10:02 AM
  • Do you get any error message?

    Best regards, Anders - MCP/CCNP

    Tuesday, August 15, 2017 10:04 AM
  • Do you get any error message?

    Best regards, Anders - MCP/CCNP


    No, i don't get any error message, but didn't receive the mail either.

    Tuesday, August 15, 2017 10:07 AM
  • Try:

    $body = get-content -path "terminliste_nfps.txt" | Out-String

    Send-MailMessage -body $body -SmtpServer "smtpserver" -From "backup@example.com" -To "user@example.com" -Subject "Daily Report"

    If that doesn't work, you should probably look at the logs from the smtp-server, or use network monitor v3.4 to see if it's delivered to the smtp-server. Maby it get caught by spam-filter at your organization?


    Best regards, Anders - MCP/CCNP

    Tuesday, August 15, 2017 10:54 AM
  • This works! Great, thank you. 
    Do you know how to use parameters to give the Failed backups a description so the email says what went wrong with the backup?

    This is my fail script now that is triggered on event id's:

    $text = "$(Get-Date) CC-DC01: Backup failed"
    $text >> 'C:\Log\Logging.txt'
    Tuesday, August 15, 2017 11:07 AM
  • Great.

    I'm not familiar with parameters to get full description from the backup reports. Maybe you could ask that question in the server forum.


    Best regards, Anders - MCP/CCNP

    Tuesday, August 15, 2017 11:23 AM
  • This works! Great, thank you. 
    Do you know how to use parameters to give the Failed backups a description so the email says what went wrong with the backup?

    This is my fail script now that is triggered on event id's:

    $text = "$(Get-Date) CC-DC01: Backup failed"
    $text >> 'C:\Log\Logging.txt'

    You must pass the parameters in the event query.  Here is one of many examples of how to do this:

    https://blogs.technet.microsoft.com/wincat/2011/08/25/trigger-a-powershell-script-from-a-windows-event/


    \_(ツ)_/

    Tuesday, August 15, 2017 4:41 PM