Print Spooler

Ezad amir 1 Reputation point
2020-08-18T10:25:46+00:00

Hi, I need to create powershell to monitor print spooler which is keep on stopping. The conditions are for the ps to restart print spooler 3x before alert is being generated in scom.

Below are my initial scripts. (Sorry I'm super newbie in this).

$serviceName = 'Spooler'

$timeout = new-timespan -minutes 10

$sw = [diagnostics.stopwatch]::StartNew



If (Get-Service $serviceName -ErrorAction SilentlyContinue)

{

               If ((Get-Service $serviceName).Status -eq 'Stopped')

               {

                              Start-Service $serviceName

        Write-Host "Starting $serviceName"

               }

                              Else

               {

                              Write-Host "$serviceName is found and running."

               }

               while ($sw.elapsed -lt $timeout)

               if ((Get-Service $serviceName).Status -eq 'running')

               {

                              write-host $serviceName is running

                              return

               }

               else

               {

                              start-sleep -seconds 180

               }


}

Else

               {

               Write-Host "$serviceName not found"

               }
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,546 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue 37,541 Reputation points Microsoft Vendor
    2020-08-19T09:42:04.44+00:00

    Hi,

    As per my understanding, you want to write a warning message to SCOM once you the script has restarted the spooler service three times. Please correct me if any misunderstanding. You could simply add counter to your script.

    $counter=3
    while($counter -gt 0)
    {
        #your start spooler service code goes here
        $counter -= 1
        continue
    }
    Write-WarningMessage "Spooler Stopped"
    

    Best Regards,
    Ian


    Please remember to "Accept Answer" and upvote if the reply is helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.