Advertisement
stephanlinke

[PRTG] Check Autostart services

Jan 15th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #===========================
  2. # ___ ___ _____ ___
  3. #| _ \ _ \_  _/ __|
  4. #|  _/  / | || (_ |
  5. #|_| |_|_\ |_| \___|
  6. #    NETWORK MONITOR
  7. #-------------------
  8. # Description:     This script will iterate through the windows services that are set to automatic starting and alert
  9. #                 if they don't.
  10. # Parameters:
  11. # -ComputerName - The name of the computer you want to check for its service (ip is okay too)
  12. # -IgnoreList: The services that are ignored by the script (like google update services)
  13. # -Username: The username and the domain/computer name that applies to the target hosts
  14. # -Password: The password for the given user.
  15. # Example:
  16. # Get-Services.ps1 -ComputerName %host -Username "%windowsdomain\%windowsuser" -Password "%windowspassword" -IgnoreList "Service1,Service2"
  17.  
  18. # ------------------
  19. # (c) 2014 Stephan Linke | Paessler AG
  20. param(
  21.     [string]$ComputerName = "",
  22.     [string]$IgnoreList = "Dell Digital Delivery Service,Gruppenrichtlinienclient,Remoteregistrierung,Software Protection",
  23.     [string]$UserName = "",
  24.     [string]$Password = ""
  25. )
  26.  
  27. # Error if there's anything going on
  28. $ErrorActionPreference = "Stop"
  29.  
  30. # Generate Credentials Object
  31. $SecPasswd  = ConvertTo-SecureString $Password -AsPlainText -Force
  32. $Credentials= New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)
  33.  
  34. # hardcoded list that applies to all hosts
  35. $IgnoreScript = 'Google Update Service (gupdate),PRTG Probe Service'
  36. $IgnoreCombined = @($IgnoreList) + @($IgnoreScript)
  37.  
  38. $Ignore = $IgnoreCombined -Split ","
  39.  
  40. # Get list of services that are not running, not in the ignore list and set to automatic.
  41. Try{ $Services = Get-WmiObject Win32_Service -ComputerName $ComputerName -Credential $Credentials | Where {$_.StartMode -eq 'Auto' -and $Ignore -notcontains $_.DisplayName -and $_.State -ne 'Running'}  }
  42. # If the script runs for the PRTG server itself, we don't need credentials
  43. Catch{ $Services = Get-WmiObject Win32_Service -ComputerName $ComputerName | Where {$_.StartMode -eq 'Auto' -and $Ignore -notcontains $_.DisplayName -and $_.State -ne 'Running'}  }
  44.  
  45. if($Services){
  46.     $ServiceList = ($Services | Select -expand DisplayName) -join ", "
  47.     if(!$Services.Count){
  48.         Write-Host "1:Automatic service(s) not running:"$ServiceList
  49.         exit 1
  50.     }
  51.     else{
  52.     Write-Host $Services.Count":Automatic service(s) not running:"$ServiceList
  53.     exit 1
  54.     }
  55. }
  56. else{
  57.     Write-Host "0:All automatic services are running."
  58.     exit 0
  59. }
  60. #===========================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement