Powershell get-service runs long when server is down -
i have script use check services on remote servers, , email if service down. however, if server happens down while script running, can cause script run long time. there way speed process? script continue until completion. appreciated. also, if there else can improve on in script please let me know i'm new powershell. thanks!
#****************** function send e-mail ******************# function sendemail { send-mailmessage -to "recipient <admin@mail.com>" -from "sender <admin@mail.com>" ` -subject "$esubject" ` -body "$ebody" -smtpserver mail.smtpserver.com } #****************** set variables ******************# $erroractionpreference = "silentlycontinue" $date = get-date $servicespath = "c:\path_to_file\services.txt" $serverspath = "c:\path_to_file\servers.txt" $services = get-content $servicespath $servers = get-content $serverspath #****************** check services , send e-mail ******************# foreach ($service in $services) { foreach ($server in $servers) { #check if service exist on server $checkservice = get-service -computername $server -displayname $service #if service exists evaluate status if($checkservice) { if ($checkservice.status -notlike "running") { #get server last boot time, set e-mail subject , body, , send e-mail $operatingsystem = get-wmiobject -class win32_operatingsystem -computername $server $lastbootup = $operatingsystem.converttodatetime($operatingsystem.lastbootuptime) $esubject = "$service down on $server" $ebody = "$date`r`n$service down on $server`r`nlast bootup time $server is: $lastbootup" sendemail } } } }
before calling get-service
, check see if server online. following send 1 ping see if server online, , if not, skip next item in $servers
. put before line call get-service
.
if ((test-connection -computername $server -quiet -count 1) -eq $false) { continue; }
Comments
Post a Comment