We have a need to detect installation of new services on our application servers. These are Windows 2016 & 2019 servers. I decided to write and schedule a PS script that will run twice a day. This script will count the number of existing services in the morning. It will run again in the evening. If the services count is higher in the evening, it will create an entry in the event viewer log. This will be picked up by SCOM and alert generated.
This is what I have done so far:
$Kaizen = (Get-Service | Measure.Object).Count
$Vic = 800 #The 800 is just an arbitrary number used for testing purposes
If($Vic -gt $Kaizen)
{
Write-Eventlog -LogName "Windows Powershell" -Source 'Pwershell' -Entrytype Error
-EventId 45874 -Message "New Service detected"
}
The above script works but I would prefer that I do not use static data as in $Vic. It is preferable that the script captures the services count in the morning, store it and reuse it for comparison purpose in the evening. Secondly, we need the script to include the name or description of the new service detected in the -Message.