forked from dfranciscus/Get-PSServiceStatus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-PSServiceStatus.ps1
More file actions
89 lines (82 loc) · 2.84 KB
/
Get-PSServiceStatus.ps1
File metadata and controls
89 lines (82 loc) · 2.84 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function Get-PSServiceStatus {
param (
[string[]]$ComputerName,
[string]$ServiceName,
[string]$Path,
[string]$FromAddress,
[string]$ToAddress,
[string]$SmtpServer
)
# Test ping
workflow Test-Ping
{
param(
[Parameter(Mandatory=$true)]
[string[]]$Computers
)
foreach -parallel -throttlelimit 150 ($Computer in $Computers)
{
if (Test-Connection -Count 1 $Computer -Quiet -ErrorAction SilentlyContinue)
{
$Computer
}
else
{
Write-Warning -Message "$Computer not online"
}
}
}
$ComputerName = Test-Ping -Computers $ComputerName
foreach ($Computer in $ComputerName)
{
$NewPath = Join-Path -Path $Path -ChildPath $Computer
#Get previous status
if (Test-Path -Path $NewPath)
{
$PreviousStatus = 'Not Running'
}
else
{
$PreviousStatus = 'Running'
}
#Get current status
$CurrentStatus = Get-Service -Name $ServiceName -ComputerName $Computer | Where-Object {$_.Status -eq 'Running'}
if ($CurrentStatus)
{
$CurrentStatus = 'Running'
}
else
{
$CurrentStatus = 'Not Running'
}
#Current status running and previous up
if ($PreviousStatus -eq 'Running' -and $CurrentStatus -eq 'Running')
{
Write-Output "$Computer $ServiceName still running"
Continue
}
#Current status running and previous down
if ($PreviousStatus -eq 'Not Running' -and $CurrentStatus -eq 'Running')
{
Write-Warning -Message "$Computer $ServiceName now running"
Remove-Item -Path $NewPath -Force | Out-Null
Send-MailMessage -Body ' ' -From $FromAddress -SmtpServer $SmtpServer -Subject "$Computer $ServiceName is now running" -To $ToAddress
Continue
}
#Current status down and previous down
if ($PreviousStatus -eq 'Not Running' -and $CurrentStatus -eq 'Not Running')
{
Write-Warning -Message "$Computer $ServiceName still not running"
New-Item -Path $NewPath -ItemType File -Force | Out-Null
Continue
}
#Current status down and previous up
if ($PreviousStatus -eq 'Running' -and $CurrentStatus -eq 'Not Running')
{
Write-Warning -Message "$Computer $ServiceName is not running"
New-Item -Path $NewPath -ItemType File -Force | Out-Null
Send-MailMessage -Body ' ' -From $FromAddress -SmtpServer $SmtpServer -Subject "$Computer $ServiceName is not running" -To $ToAddress
Continue
}
}
}