2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator

PowerShell Monitor Services

<#
.Synopsis
    ScriptsbyScott.com
.DESCRIPTION
    Goes into Active Directory and pulls a list of servers that are not disabled and return a ping
    The script then checks to see if a list of specific services are running and sends an alert to
    user in case the backup services are not online. There is also a Try / Catch for failures that 
    sends alerts to user. The Script sends 1 email each day to user either all OK or with attached
    HTML file with list of servers and services with issues.
.EXAMPLE
    Requires:
        Rights on Remote Machines 
        Exclusion file
        Active Directory Module 
        Setup using Task Scheduler
        Directory C:\temp 
#>

Function Acronis{    

    Begin{             

               

#Delete File            

If(Test-path "C:\temp\Acronis.html"){Remove-Item "C:\temp\Acronis.html"}      
               

#//////////////////////////Pull Exclusion List\\\\\\\\\\\\\\\\\\\\\\\\\\\\\                

$Exclusion_List = Get-Content D:\Scripts\Exclusion.txt

               

#//////////////////////////Pull All Enabled Windows Servers\\\\\\\\\\\\\\\\                

$Servers=Get-ADComputer -filter {(Operatingsystem -like "Windows Server*" -and Enabled -eq $True)} -Properties *  -ErrorVariable A -ErrorAction SilentlyContinue | Select -ExpandProperty Name

               

#////////////Compare the two sources and remove all items matched in the exclusion file list\\\\\\\\\\\\\\\\\\\                

$Exclusions_Remvoed_List=Compare-Object -ReferenceObject $Servers -DifferenceObject $Exclusion_List | Select -ExpandProperty inputobject -ErrorVariable A -ErrorAction SilentlyContinue

               

#//////////////////Test Connection to Multiple Systems at Once\\\\\\\\\\\\\
$Array=@()        
$Array += $Exclusions_Remvoed_List | % {new-object psobject -Property @{Computername=$_; Reachable=(test-connection -ComputerName $_ -Quiet -Count 1)}} | Where-Object {$_.Reachable -eq "True"} | Select -ExpandProperty ComputerName -ErrorVariable A -ErrorAction SilentlyContinue               
                
 #//////////////////Get the service status by displayname\\\\\\\\\\\\\\\\\\\\\\\\\\\\       
 $Acronis1={
               

Get-Service -ComputerName $ENV:computername -Name "Acronis Remote Agent Service" -ErrorVariable A -ErrorAction SilentlyContinue | ?{$_.Status -eq 'Stopped'} | Start-Service 
               

Get-Service -ComputerName $ENV:computername -Name "Acronis Scheduler2 Service" -ErrorVariable A -ErrorAction SilentlyContinue  | ?{$_.Status -eq 'Stopped'} | Start-Service 
               

Get-Service -ComputerName $ENV:computername -Name "Acronis Managed Machine Service" -ErrorVariable A -ErrorAction SilentlyContinue  | ?{$_.Status -eq 'Stopped'} | Start-Service

               

Get-Service -ComputerName $ENV:computername -Name "Acronis Remote Agent Service" -ErrorVariable A -ErrorAction SilentlyContinue| Select DisplayName,Status, Machinename
               

Get-Service -ComputerName $ENV:computername -Name "Acronis Scheduler2 Service" -ErrorVariable A -ErrorAction SilentlyContinue| Select DisplayName,Status, Machinename                               
               

Get-Service -ComputerName $ENV:computername -Name "Acronis Managed Machine Service" -ErrorVariable A -ErrorAction SilentlyContinue| Select DisplayName,Status, Machinename
                }        
                 
         
        }#End Begin 
        
        Process{ 
          
                #////////Execute Acronis1 on list of servers in $Array\\\\\\\\\
                $Acronisx=@()
                $Acronisx=invoke-command -ComputerName $Array -scriptblock $Acronis1 -ErrorVariable A -ErrorAction SilentlyContinue
        
#//////////Filter looking for all services not equal to running\\\\\\\\\\\\ 
$MyData=$Acronisx | Where {$_.status.tostring() -ne "Running"} | Select MachineName, Displayname, Status -ErrorVariable A -ErrorAction SilentlyContinue
        
        }#End Process 

       End{  #IF ELSE XYZ        
             If($MyData -ne $Null){

                $MyData | ConvertTo-Html | Out-file "C:\temp\Acronis.html"

                #////////////Send Email Alert\\\\\\\\"
                $SmtpServer = "mail.server.com"
                $SMTPFrom="User@Server.com"             
                $SMTPto="User@Server.com"
                $SMTPSubject= "Acronis Service Offline"
                $SMTPBody= "Acronis Service Check SCRIPT from server See attached file"
                Send-Mailmessage -SmtpServer $SmtpServer -from $SMTPFrom -to $SMTPto -Subject $SMTPSubject -Body $SMTPBody -Attachments "C:\temp\Acronis.html" 

            }else{

                #////////////Send Email Alert\\\\\\\\"
                $SmtpServer = "mail.server.com"
                $SMTPFrom="User@Server.com"             
                $SMTPto="User@Server.com"
                $SMTPSubject= "Acronis Services OK"
                $SMTPBody= "Acronis Service Check SCRIPT from server"
                Send-Mailmessage -SmtpServer $SmtpServer -from $SMTPFrom -to $SMTPto -Subject $SMTPSubject -Body $SMTPBody  
              
            }#End If Else XYZ
            
        }#End End 
}#End Function                                
   


Try{

    Acronis -errroraction stop 

}Catch{
            
                #/////Send Alert if Script Errors\\\\\\\
                $SmtpServer = "mail.server.com"
                $SMTPFrom="User@Server.com"             
                $SMTPto="User@Server.com"
                $SMTPSubject="- Script Issue - $A" + $_
                $SMTPBody="Begin Script Issue - Technical Notes: Script Location - D:\Scripts\Monitor.ps1"
                Send-Mailmessage -SmtpServer $SmtpServer -from $SMTPFrom -to $SMTPto -Subject $SMTPSubject -Body $SMTPBody        

}#End Try Catch