-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest-Service.ps1
65 lines (53 loc) · 1.83 KB
/
Test-Service.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.Synopsis
Tests and corrects the status of a service.
.DESCRIPTION
Determines if a service is running or stopped. If stopped, Test-Service will attempt to start.
.EXAMPLE
C:\> Test-Service -ComputerName DC01,DC02,DC03 -Service DNS
VERBOSE: [DC01] Testing DNS service
VERBOSE: [DC01] PASSED
VERBOSE: [DC02] Testing DNS service
VERBOSE: [DC02] PASSED
VERBOSE: [DC03] Testing DNS service
WARNING: [DC03] FAILED
WARNING: [DC03] Starting DNS service
VERBOSE: [DC03] PASSED
#>
function Test-Service
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[String[]]$ComputerName,
[Parameter(Mandatory=$true)]
[String[]]$Service
)
Begin
{
$VerbosePreference = 'Continue'
}
Process
{
foreach ($Server in $ComputerName) {
Write-Verbose "[$Server] Testing $Service service"
$Status = Get-Service -ComputerName $Server -Name $Service
if ($Status.Status -eq 'Running') {
Write-Verbose "[$Server] PASSED" }
if ($Status.Status -eq 'Stopped') {
Write-Warning "[$Server] FAILED"
Write-Warning "[$Server] Starting $Service service"
Invoke-Command -ComputerName $Server -ScriptBlock {param($svc) Start-Service $svc} -ArgumentList $Service
$Status = Get-Service -ComputerName $Server -Name $Service
if ($Status.Status -eq 'Running') {
Write-Verbose "[$Server] PASSED" }
if ($Status.Status -eq 'Stopped') {
Write-Warning "[$Server] Cannot start $Service service" }
}
}
}
End
{
$VerbosePreference = 'SilentlyContinue'
}
}