Top 10 PowerShell Script to check the Windows Service on remote server

 Top 10 PowerShell Script to check the Windows Service on a remote server

PowerShell is an object-oriented automation engine and scripting language with an interactive command-line shell that Microsoft developed to help IT professionals configure systems and automate administrative tasks.

As an IT admin professionals, daily we need to deal with Windows Services which is available in control panel (services.msc). Most of the application having services which need to start and stop on need basic which is quite easy from GUI, but in this article, we will cover commands and scripts by which we can control all Windows services from one server to remote servers.

If you are managing lots of services it is difficult to manage by clicking on  GUI, by using PowerShell automation scripts we can efficiently manage all these different services and even explore much more things, like the order of start and stop services, scheduling, enable and disable services etc.

The Get-Service command allows you to get information about installed services on your server, by using Get-Service PowerShell command you can even get services details of the remote server also without even log in to serve.

By using this commands with the different filter, loops, and condition we can generate the meaningful reports either in CSV, HTML or text formate. we can manage different remote servers services from one place.

Get-Service command examples:-

1. Show all the running services on the server.

 Get-Service | Where {$_.status –eq 'running'}

2. Get the service from (SERVICE-NAME) in SERVER1, SERVER2 with formate table.

 Get-Service -ServiceName *SERVICE-NAME* -ComputerName SERVER1, SERVER2 | select name, status, machinename | sort machinename | format-table -AutoSize

3. Get the service status form the list of server name store in MyServers.txt.

 Get-Service -computername (get-content c:\folder\Servers_list.txt) | Select servername,status |sort name | format-table -autosize<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>

4. Get server from wuauserv from listed VM’s.

 'vm1','vm2','vm3'| foreach {get-service
wuauserv -computername $_} | Format-Table Name,Status,Machinename -AutoSize

5. Run the service from the listed remote server.

 Get-Service -Name express* -Computer vm1, vm2, vm3 |
? { $_.Status -eq "Running" } |
format-table -auto MachineName, ServiceName, DisplayName, Status

6. Find out from the list of server in which one SQL is installed and export in csv file.

$Machines = Get-Content -Path "C:\machines.txt"
foreach ($computer in $machines){
Write-host "SQL Service chacking on $computer" -b "green" -foregroundcolor "red"
Get-Service -Displayname "*SQl Server*" -ComputerName "$computer"
}
 ps >.\sql_service.ps1 | Export-Csv C:sql_install_server.csv -encoding "unicode"

Running Multiple different services on different servers

7. Starting Multiple services on different servers.

Create a 2 txt file and save one as services.txt which is the list of service one by one and in machines.txt list server one by one

services.txt >

wuauserv
MpsSvc
W32Time
W3SVC
wmiApSrv

machines.txt >

vm1
vm2
vm3
vm4
vm5

Next, download PsServices.exe and save to C : drive from where you are running your script.

 $Services = Get-Content -Path "C:\services.txt"
$Machines = Get-Content -Path "C:\machines.txt"
foreach ($computer in $machines){
Write-host "Service is going to start on $computer" -b "green" -foregroundcolor "white"
foreach ($service in $services) {
PsService.exe \$computer start $service
}
}

8. The same script can be performed well by using Get-Service

$Services = Get-Content -Path "C:service_list.txt"
$Machines = Get-Content -Path "C:computers_list.txt"
foreach ($computer in $machines){
Write-host "Service is going to start on $computer" -b "green" -foregroundcolor "red"
foreach ($service in $services) {
Get-Service -ComputerName $computer -Name $service
}
}

9. Run script from the list.csv file :

list.csv >

 Hostname Services
vm1 VMTools
vm2 VMTools
vm2 wuauserv
Import-Csv C:\list.csv -Delimiter "`t" | % {
$services = $_.Services -split ','
echo -Computer $_.Hostname -Name $services
Start-Service -Computer $_.Hostname -Name $services
}

10. Multiple unique services Stop and Start to a different server.

Save text file server.txt with below details:

server.txt >

  ComputerName,Service
vm1,"VMTools,wuauserv"
vm2,""MpsSvc,Schedule"
$List = Import-CSV c:\servers.csv

ForEach ($Server in $List)
{ Invoke-Command -ScriptBlock { Stop-Service $args[0] } -ComputerName $Server.ComputerName -ArgumentList $Server.Service.Split(",")
}
Start-Sleep -Seconds 6

ForEach ($Index in (($List.Count - 1)..0))
{ Invoke-Command -ScriptBlock { Stop-Service $args[0] } -ComputerName $List[$Index].ComputerName -ArgumentList $List[$Index].Service.Split(",")

Hope You Enjoy the Article.

You May Also Like