Powershell Script to restart Windows Service If it is Down and send email -
i have powershell script monitors status of windows services on multiple servers. can me few modification script checks status , restarts service if not running? send 1 consolidated email.
code below
$servers = get-content c:\servers.txt $servers | foreach-object { invoke-command -computername $_ -scriptblock {get-service w3svc} }| out-file c:\result.txt
help appreciated.
i modified te script again , working me now.
#$services=get-content c:\services.txt $servers=get-content c:\servers.txt $smptserver = "smtp.ac.com" $result = "the following service restarted:`n" # default output $servers|foreach-object{ $result += invoke-command -computername $_ -scriptblock { # add output of scriptblock $result $compname = (get-wmiobject -class win32_computersystem).name (get-service -name splunkd)| #get services running on servers where-object{$_.status -eq "stopped"}| #which status equal stopped foreach-object{ if($_.status -eq "stopped") { write-output "`n -service:- splunkd down on server $compname - service splunkd on server $compname restarted `n" # action added output $_.(start-service splunkd) #try start #start-sleep -seconds 60; #wait 1 minute #$_.refresh(); #refresh service update status }}}} if ($result -ne "the following service restarted:`n") { # if action taken send mail $result send-mailmessage -to "xx@xx.com" -from "abc@bc.com" -subject "splunkd service restarted alert" -body $result -smtpserver $smptserver }
Comments
Post a Comment