k3s.live

Based on the IT journey of Michael Rickert

Quick powershell tips

Just posting a quick script I set up using powershell that restarts a windows service when it would get stuck…

In my environment there was a windows service that, when stuck, would dump log files into a network folder for later processing. In the past when the service got stuck, someone would have to log into the server and manually restart the service, but I decided to use Windows powershell to have a script automate that job.

The script used reads as follows:

 If ( (Get-ChildItem \\N:\outbound\XML).Count -gt 5 ){
Restart-Service ProcessorSvc2;
}

The first half of the script,  If ( (Get-ChildItem \\N:\outbound\XML).Count -gt 5,  checks a network folder called outbound\XML and when it sees more than 5 files in that folder, the next part of the script will trigger. The next part being  Restart-Service ProcessorSvc2; which restarts the service ‘ProcessorSvc2’. When the service restarts, it will process the backed-up log files in the XML folder and the script will go back to monitoring the folder.

The powershell script is set to run on boot via taskscheduler after 15 minutes of boot time and will run whether the user tied to the script is logging in or not, thus automating a task that used to take valuable minutes away from a sysadmin when they were focused on other projects.

Leave a Reply