Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

I need an alert to be sent if a function fails

$
0
0

Hi all,

I'm learning powershell (slowly) and have managed to get a script working which utilizes wevtutil.exe to pull out the event logs from servers we specify.

The script creates a temporary data-table to store the results of wevtutil and then writes them directly to a SQL database.

The script iterates through a server array and remotely connects to each server - this all works as expected.

My last amendments are to ensure that the script will run consistently at times we will specify through Windows Tasks.  In the fist instance I've added a "Test-Connection" function which will execute the commands IF the server responds and if not, it will send-email.  This will ensure that we're alerted when a server is offline.

The last error checking I want is to alert when the server is online BUT the script fails for whatever reason (server timed-out, server was switched off during run, etc).

Does anyone know how I can achieve this?  Below is the current error checking code:

#ScriptName = EventLogCollection.ps1

$Serverarray = ("Server1","Server2","Server3")

$Serverarray | foreach

{

$Server = $_

if(Test-Connection -ComputerName $Server -Count 1 -ErrorAction 0)

{

Write-Host "Writting Logs for $Server"

#Calls the function to execute the wevtutil command above

WriteEventLogsToSQL

Write-Host "Done Writing Logs for $Server"

#Email action to go here

}

else

#Email action to go here

{Write-Host "$Server is not Available" -ForegroundColor red}

#Here should go alerts where the Function fails

}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>