I need an alert to be sent if a function fails


hi all,

i'm learning powershell (slowly) , have managed script working utilizes wevtutil.exe pull out event logs servers specify.

the script creates temporary data-table store results of wevtutil , writes them directly sql database.

the script iterates through server array , remotely connects each server - works expected.

my last amendments ensure script run consistently @ times specify through windows tasks.  in fist instance i've added "test-connection" function execute commands if server responds , if not, send-email.  this ensure we're alerted when server offline.

the last error checking want alert when server online script fails whatever reason (server timed-out, server switched off during run, etc).

does know how can achieve this?  below 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 $server"

#calls function execute wevtutil command above

writeeventlogstosql

write-host "done writing logs $server"

#email action go here

}

else

#email action go here

{write-host "$server not available" -foregroundcolor red}

#here should go alerts function fails

}

you use try-catch error handling constructs (and possibly throw custom exception in example writeeventlogstosql function.

see page: http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx or one: http://technet.microsoft.com/en-us/library/dd315350.aspx

rough example:

$serverarray = ("server1","server2","server3")  $serverarray | %{ 	$server = $_ 	if(test-connection -computername $server -count 1 -erroraction 0) {		 		try { 			write-host "writing logs $server"		 			# calls function execute wevtutil command above 			writeeventlogstosql	 			write-host "done writing logs $server" 			# complete success email action go here 		} catch [system.exception] { 			write-host "some error occured server online when tested!" 			# online not complete success email action go here 		} { 			 		}				 	} 	else 	{ 		write-host "$server not available" -foregroundcolor red 	} }



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

RDS 2012 r2 collections show black screen and then close