Overview
On Windows platform, managing services with windows PowerShell is easier than earlier. PowerShell provides cmdlet to manage services for both local and remote servers. In order to manage Services you need to utilize “Get-Service” cmdlet.In this post and snippet we will demonstrate, listing services configured on localhost and listing them according to its service state that are stopped and running on the server.
Applies To
Windows 10, Windows 2008 R2, Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Policy Type | Purpose |
---|---|
Restricted | No scripts can be run. Windows PowerShell can be used only in interactive mode. |
AllSigned | Only scripts signed by a trusted publisher can be run. |
RemoteSigned | Downloaded scripts must be signed by a trusted publisher before they can be run. |
Unrestricted | No restrictions; all Windows PowerShell scripts can be run. |
PowerShell Script - Get Service Status
This script will list all the services configured on the server along with current status.PowerShell Code Snippet
# # Get Service Status sorted by Status and Name # $AllServices = Get-Service | Sort-Object Status, Name Clear-Host foreach ($SVC in $AllServices) { if ( $SVC.status -eq "Stopped") { Write-Host $SVC.name, "`t" $SVC.status -ForegroundColor Red } else { Write-Host $SVC.name, "`t" $SVC.status -ForegroundColor Green } }
Powershell Script Output
In this example we will restrict the output to only first 10 and excluding status in “Get-Service” as below snippet;$AllServices = Get-Service | Sort-Object Name | select -First 10
No comments:
Post a Comment