Get service on remote server


posted by Sylvain LESIRE
03-24-2009

Downloads: 1,277
File size: 901 B
Views: 18,066

Embed
Get service on remote server
  1. # this script permits to get service info for a set of remote server  
  2.  
  3. # syntax 
  4. #get-content .\MyServerList.txt | get-serviceonRemoteServer.ps1 
  5.  
  6. #command line to export-csv 
  7. #get-content .\MyServerList.txt | get-serviceonRemoteServer.ps1 | export-csv .\MyServiceResult.csv -noTypeInformation 
  8.  
  9. #Comand line to generate an HTML file 
  10. #get-content .\MyServerList.txt | get-serviceonRemoteServer.ps1 | convertToHtml | out-file .\MyServiceResult.Html 
  11.  
  12.  
  13.  
  14. process{ 
  15. $servername = $_ 
  16. $serviceArray = Get-WmiObject -class Win32_Service -computerName $servername 
  17. $temp = @()   
  18.  
  19. $serviceArray | foreach
  20.     $serviceinfo = "" | select servername, name, startmode, state, status 
  21.  
  22.     $serviceinfo.servername = $servername 
  23.     $serviceinfo.name = $_.name 
  24.     $serviceinfo.startmode = $_.startmode 
  25.     $serviceinfo.state = $_.state 
  26.     $serviceinfo.status = $_.status 
  27.      
  28.     $temp +=$serviceinfo 
  29.     
  30. $temp  

This script permits to give you the status , state, and start mode for a set of remote server

# syntax

#get-content .\MyServerList.txt | get-serviceonRemoteServer.ps1

#command line to export-csv

#get-content .\MyServerList.txt | get-serviceonRemoteServer.ps1 | export-csv .\MyServiceResult.csv -noTypeInformation

#Comand line to generate an HTML file

#get-content .\MyServerList.txt | get-serviceonRemoteServer.ps1 | convertToHtml | out-file .\MyServiceResult.Html

See You

Comments

Aleksandar wrote re: Get service on remote server
on 03-25-2009 9:52 AM

Why don't you use just a simple one-liner:

gwmi win32_service -comp (gc servers.txt) |

select __server,name,startmode,state,status

You can assign a result to a variable and do with it whatever you like.

Bernard wrote re: Get service on remote server
on 03-27-2009 4:12 PM

nice one Aleksandar :)

glmwpg wrote re: Get service on remote server
on 03-31-2009 4:23 AM

Would it be possible to expand on the script idea a bit - by searching for any started/running services with the word "Blackberry" in it, and if so stopping then starting the service, then moving on to the next service with the word Blackberry in it? If Blackberry services are found but disabled, leave them as they are.

Sylvain LESIRE wrote re: Get service on remote server
on 04-13-2009 2:30 PM

it s true !!!  Aleksandar

;)

Aleksandar wrote re: Get service on remote server
on 04-14-2009 5:18 AM

glmwpg,

Here is something for you:

# first we stop the running services

gwmi win32_service -comp (gc servers.txt) -filter "state='running' and name like '%Blackberry%'" | % {$_.StopService()}

# wait 30 seconds for services to stop

Start-Sleep 30

# then we start stopped services

gwmi win32_service -comp (gc servers.txt) -filter "state='stopped' and name like '%Blackberry%'" | % {$_.startservice()}

# wait 10 seconds for services to start

Start-Sleep 10

# check the state of services

gwmi win32_service -comp (gc servers.txt) -filter "name like '%Blackberry%'" | select __server,name,startmode,state,status

If you try to pipe last result to format-* cmldets you will get an error:

out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not legal or not in the corre

ct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting.

This is a known issue. :(

Copyright 2012 PowerShell.com. All rights reserved.