• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Scripts / Email error variable PowerShell

Email error variable PowerShell

05/01/2018 by Stephanos 12 Comments

Email error variable PowerShell

Scenario:

Email error variable PowerShell.

I run a lot of scripts on a schedule basis and I had to check manually of the scripts run successfully. Sometimes a  problem in the configuration of a user or group may result in an error while the script is running to perform any changes. If there is an error, either the script runs successfully without applying any changes or the scripts completely fails. I came up with the below solution as a first countermeasure to overcome the above issues.

What the script does actually, is that it takes the default variable of PowerShell called $error and sends it in an email. If there are multiples errors during the run of the script the error variable is an array that holds all the values (errors / exceptions), created during that time. By using Foreach loop, I retrieve all the values and place them in an html variable, so I will be able to send them by email to me. If the error variable is empty then nothing is send.

Related Links:

  • PowerShell Scripts
  • PowerShell Tutorials
  • Send-MailMessage – Microsoft Docs
  • Send Email using PowerShell
  • Encrypt password with key using PowerShell
  • Get-Content – Microsoft Docs
  • ConvertTo-SecureString – Microsoft Docs
  • New-Object – Microsoft Docs
  • Get-Date – Microsoft Docs

Solution / Script:

$File = "C:\Scripts\Password.txt"
$Key = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32)
$EmailUser = "Script-User@domain.com"
$Password = Get-Content $File | ConvertTo-SecureString -Key $Key
$EmailCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $EmailUser,$Password
$To = 'User1@domain.com'
$From = 'Script-User@domain.com'
$EmailResult = ""
if ($error -ne $null) {
    foreach ($value in $error) {
    
        $ErrorEmailTemp = @"
        <tr>
            <td class="colorm">$value</td>
        </tr>
"@
        $ErrorEmailResult = $ErrorEmailResult + "`r`n" + $ErrorEmailTemp
    }
    
    $ErrorEmailUp = @"
    <style>
        body { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; color:#434242;}
        TABLE { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
        TR {border-width: 1px;padding: 10px;border-style: solid;border-color: white; }
        TD {font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;padding: 10px;border-style: solid;border-color: white; background-color:#C3DDDB;}
        .colorm {background-color:#58A09E; color:white;}
        .colort{background-color:#58A09E; padding:20px; color:white; font-weight:bold;}
        .colorn{background-color:transparent;}
    </style>
    <body>
    
    <h3 style="color:#BD3337 !important;"> WARNING!!!</h3>
    
    <p>There were errors during users check</p>
    <p>Please check the errors and act accordingly</p>
    <table>
"@
    $ErrorEmailDown = @"
    </table>
    </body>
"@
    $ErrorEmail = $ErrorEmailUp + $ErrorEmailResult + $ErrorEmailDown
    
    send-mailmessage `
        -To $To `
        -Subject "Users and their Groups Report $(Get-Date -format dd/MM/yyyy) - WARNING" `
        -Body $ErrorEmail `
        -BodyAsHtml `
        -Priority high `
        -UseSsl `
        -Port 587 `
        -SmtpServer 'smtp.office365.com' `
        -From $From `
        -Credential $EmailCredentials
}
Summary
Email Error Variable PowerShell
Article Name
Email Error Variable PowerShell
Description
Email error variable PowerShell. When you are running scripts you want to check if there were any errors during that time and if everything was successful.
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Scripts Tagged With: Arithmetic Operators, Comparison Operators, ConvertTo-SecureString, Get-Content, Get-Date, Microsoft Office 365, New-Object, Send-MailMessage

Reader Interactions

Trackbacks

  1. Set Active Directory user attributes automatically with PowerShell - Stephanos Constantinou says:
    11/01/2018 at 13:28

    […] Email error variable in PowerShell […]

    Reply
  2. Send Email using PowerShell - Stephanos Constantinou says:
    11/01/2018 at 13:29

    […] the script, if there are any, and also to be informed in case of an error (there will be a separate post on error reporting emails). The below uses the Exchange Online to send the email to the intended […]

    Reply
  3. PowerShell Get registry value data for backups - Stephanos Constantinou says:
    15/01/2018 at 12:50

    […] Email error variable in PowerShell […]

    Reply
  4. Remove Forwarding Office 365 PowerShell - Stephanos Constantinou says:
    18/01/2018 at 11:35

    […] Email error variable in PowerShell […]

    Reply
  5. Enable Unified Messaging Office 365 using PowerShell. - Stephanos Constantinou says:
    22/01/2018 at 15:45

    […] Email error variable in PowerShell […]

    Reply
  6. PowerShell List all Users and Group Membership - Stephanos Constantinou says:
    25/01/2018 at 13:23

    […] Email error variable in PowerShell […]

    Reply
  7. Office 365 Litigation Hold with Powershell - Stephanos Constantinou says:
    29/01/2018 at 12:18

    […] Email error variable in PowerShell […]

    Reply
  8. Enable mailbox audit logging Office 365 with PowerShell - Stephanos Constantinou says:
    31/01/2018 at 15:11

    […] Email error variable in PowerShell […]

    Reply
  9. Check if AD group is in correct OU - Stephanos Constantinou says:
    22/02/2018 at 10:13

    […] Email error variable in PowerShell […]

    Reply
  10. Exchange custom attributes on groups automatically with PowerShell - Stephanos Constantinou says:
    22/02/2018 at 10:22

    […] Email error variable in PowerShell […]

    Reply
  11. Office 365 PowerShell License Report - Stephanos Constantinou says:
    22/02/2018 at 12:47

    […] Email error variable in PowerShell […]

    Reply
  12. Office 365 License Report - Part 2 - Stephanos Constantinou Blog says:
    03/05/2018 at 12:41

    […] Email error variable in PowerShell […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact