question

GCM313 avatar image
0 Votes"
GCM313 asked RichMatheisen-8856 commented

Disable Print Spooler Script

I created the script below to scan my list of computer and stop and disable print spooler service. It generates report, showing the service was stopped, but i check a computer on the list and it shows as running. Is something off with the script?

 $computers = Get-Content -Path "C:\temp2\ComputerList.txt"
 foreach ($computer in $computers) {
     if(!(Test-Connection -ComputerName $computer -Count 1 -Quiet))
     {
         Write-Output "$computer is offline"
         continue
     }
     $service = Get-Service -name Spooler -computername $computer
     $ServiceStatus = $service.Status
     $ServiceDisplayName = $service.DisplayName
        
     if ($ServiceStatus -eq 'Running') {
         write-output "$ServiceDisplayName is $ServiceStatus on $computer and needs to be disabled" | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
         Stop-Service -Name Spooler -Force -PassThru | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
         Set-Service -Name Spooler -StartupType Disabled
     }
     else {
         write-output "Status of $ServiceDisplayName is $ServiceStatus on $computer" | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
     }
 }


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Do you run any type of monitoring service that might be restarting the service?

Does the service have any "Recovery" options? You'll have to use the MMC to look for that, it's not exposed by either Get-Service or WMI.

Is the services StartupType set to "disabled"? You might try setting the startuptype before you stop the service.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BradM-8788 avatar image
0 Votes"
BradM-8788 answered RichMatheisen-8856 commented

I believe the problem is line 14 and 15. You are stopping and setting the local spooler to disabled. You need to point back to the computer. Try this:

Stop-Service -InputObject $(Get-Service -Computer $computer -Name spooler) -Force -PassThru | Out-File -FilePath C:\temp2\RunningSpooler.txt -Append
Set-Service -InputObject $(Get-Service -Computer $computer -Name spooler) -StartupType Disabled

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can shorten that a bit. He's already got the output of Get-Service in the variable $service.

 $service | Stop-Service  -Force | Out-File -FilePath c:\temp2\RunningSpooler.txt -Append
 $service | Set-Service  -StartupType Disabled
0 Votes 0 ·