Been racking my brain for quite a while trying to devise a scripted solution to my problem. We've got a particular application that runs over about 10 servers, and each server has a plethora of services running on them that may or may not have issues starting every night. Each night, we have a job that runs that restarts all the services, but once in a while a service on one of the servers will get hung in a "Starting" state. I'd like to script a solution that checks a list of services running on a list of servers, and if one or more are found to be not started, then I'd like to kill that process, start the service again, and send an email to myself.
I have gotten bits and pieces of the script to work, but not all together as a cohesive unit. So I'd rather just start over from a more basic script and see if I can learn how to engineer this thing the right way (with the help of the community).
I'm starting with a Get-Content command that gets my list of servers via txt file into a $Servers array variable.
The next leg is part of what I'm not understanding. Rather than creating another array variable of a static list of service names from a text file, I'd like to just say "any service that starts with this string". Since all of our application's services start with the same two letters, I'd like to only focus on those services, and not the rest of the services on the machines (as there are several other services unrelated to our application that aren't running, and don't need to be). Is something like this possible? I'm thinking along the lines of:
$Services = Get-Service -ComputerName $Servers -Name "AB*"
But I don't think it's working in practice how I'm picturing it in my head. Any scripting experts out there that can help me out? I just want a simple solution that will:
1) Loop through an array of services within an array of servers
2) If a service is found to be in a status other than "running", kill the process and start the service
3) E-mail myself an alert telling me which service on which server was restarted
I'm pretty confident I've already got pieces for 2 and 3, it's the looping logic that is confusing me. Thanks in advance for taking the time to read this and possibly assist me.
-Chris