0

I created the powershell 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
     }
 }
gm89
  • 9
  • 1
  • 1
    You are stopping/disabling the service on your computer, not the computer you asked the status from. Take a look at [this](https://stackoverflow.com/a/10343021/52598) answer for pointers on how to remotely stop a service. – Lieven Keersmaekers Jul 14 '21 at 19:00
  • 1
    Ah, [this](https://learn-powershell.net/2012/01/15/startingstopping-and-restarting-remote-services-with-powershell/) might work as wel - `Stop-Service -InputObject $service -Verbose` – Lieven Keersmaekers Jul 14 '21 at 19:05
  • @LievenKeersmaekers Thanks got it to work using "Stop-Service -InputObject $service -Verbose" – gm89 Jul 14 '21 at 19:39

0 Answers0