-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGet-Services.ps1
49 lines (39 loc) · 1.07 KB
/
Get-Services.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<#
.SYNOPSIS
Get a list of services ordered by status and name.
.PARAMETER Name
The name or wildcard to match on the Name property of each service.
.PARAMETER Running
Only show services that are running.
.PARAMETER Stopped
Only show services that are not running.
#>
param (
[string] $Name = '*',
[switch] $Running,
[switch] $Stopped
)
if ($Running)
{
$services = Get-Service $Name | ? { $_.Status -eq 'Running' }
}
elseif ($Stopped)
{
$services = Get-Service $Name | ? { $_.Status -ne 'Running' }
}
else
{
$services = Get-Service $Name
}
$width = $host.UI.RawUI.WindowSize.Width - 46
$services | Sort-Object -Property @{ Expression='status'; Descending=$true }, name | % `
{
$c = if ($_.Status -eq 'Running'){ 'blue'} else {'darkgray'}
$sname = $_.Name
if ($sname.Length -gt 35) { $sname = $sname.Substring(0,35) }
Write-Host $sname.PadRight(35,' ') -NoNewline -ForegroundColor $c
Write-Host $_.StartType.ToString().PadRight(10,' ') -NoNewline
$dname = $_.DisplayName
if ($dname.Length -gt $width) { $dname = $dname.Substring(0, $width - 3) + '...' }
Write-Host " $dname"
}