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.powershell get service remote computer
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:-
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.
Post Views:
4,530
Top 10 PowerShell Script to check the Windows Service on 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.
Editor's Rating:
4