Remote Service commands - multiple services/servers?

Welcome Forums General PowerShell Q&A Remote Service commands - multiple services/servers?

Viewing 3 reply threads
  • Author
    Posts
    • #14378
      Participant
      Topics: 5
      Replies: 3
      Points: 0
      Rank: Member

      Hey Scripting Experts!

      I'm trying my 1st script out, well my 1st that I'm trying to write where I haven't found something online to put it together with. I did find information on how to disable services, but I can't find anything to do this for multiple services across multiple servers.
      Not all of the servers will have all of the services. I want to try to get some "results" logged if possible.

      Here is what I have to disable the multiple services, but how can I do this remotely and on multiple servers, either 1 server at a time or in parallel?
      $ListOfServices = Get-Content E:\Powershellscripts\DisableTheseServices.txt
      foreach ($service in $ListOfServices)
      {
      Set-Service -Name $service -StartupType Disabled
      Stop-Service -Name $service –Force
      Get-WmiObject Win32_Service |Where-Object {$_.name -eq $Service} | Format-List -Property Name,Startmode,State
      }

    • #14388
      Participant
      Topics: 11
      Replies: 145
      Points: 48
      Rank: Member

      So the Get-Service command lacks the property of the startmode so you have to resort to WMI to see that.

      However the stupid thing is that the Set-Service command does allow you to do this and will handle piped in object names.

      You're basically executing in the blind. The best thing I can recommend is use your Get-Content with your text file and pipe that to the Set-Service command and use the -Verbose switch it will display confirmations in yellow I believe.

      Get-Content E:\Powershellscripts\DisableTheseServices.txt | Set-Service -StartupType Disabled -Verbose

      -VERN

    • #16115
      Participant
      Topics: 11
      Replies: 145
      Points: 48
      Rank: Member

      [code language="powershell"]
      Get-Content E:\Powershellscripts\DisableTheseServices.txt | Set-Service -StartupType Disabled -Verbose
      [/code]

    • #16124
      Participant
      Topics: 11
      Replies: 52
      Points: 0
      Rank: Member

      According to its help file, the Name parameter of Set-Service doesn't accept multiple values, so you have to use foreach. I can't test this right now and I don't want to mess with services on my laptop, but that's what I'd try:

      $services = Get-Service -Name (Get-Content E:\Powershellscripts\DisableTheseServices.txt)
      
      foreach ($service in $services)
      {
      Stop-Service -Name $service –Force
      Set-Service -Name $service -StartupType Disabled -Verbose
      }
      

      To remotely do this on multiple computers, you have to use Invoke-Command and its -ComputerName and -ScriptBlock or -FilePath parameters. The FilePath parameter makes it possible to run a local script on remote computers, so you could save it as a script and do that.
      However, you may have to define some arguments and I'm not sure how to make it work.
      If a foreach one-liner could accomplish the task, you'd just put that into the ScriptBlock parameter.
      In either case, you can feed in the remote computer names into the ComputerName parameter with Get-Content or by typing a list of names.

Viewing 3 reply threads
  • The topic ‘Remote Service commands - multiple services/servers?’ is closed to new replies.