Using PowerShell to send a warning email if a long-running script has thrown an error

If you're like me, you may need to run long-running PowerShell scripts to do various things in SharePoint, like updating large SharePoint lists, creating lots of list items, migrating data etc.

Typically such scripts run over a weekend. Now, you don't want to be monitoring your script all weekend - that's no fun - but it would be nice to get an email to your Inbox warning you if something has gone wrong.

The below PowerShell snippet shows how to do this. As long as you have a try - catch block around your PowerShell Code, then if an error occurs, the code in the 'catch' block below will send an email to the specific recipients, warning you to check / fix your process.

Nice - a lot less monitoring needed as a result :-)

Similarly, you might want to send yourself an email every X iterations of your process, assuring you things are still running OK. Again, some very simple PowerShell to achieve this is shown below - assuming you have a loop counter, the first line in the code below will trigger the sending of an email every 1,000 iterations of your loop.

7 views0 comments