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 for business | Windows Server | User experience | PowerShell
0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    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.

    Was this answer helpful?


Your answer

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