-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestart-WindowsService.ps1
67 lines (54 loc) · 2 KB
/
Restart-WindowsService.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
66
67
# Restart-WindowsService.ps1
# Purpose: Restart a windows service and send a notification post to Slack.
# Originally intended to be run by a scheduled task.
# Usage: .\Restart-WindowsService.ps1 -ServiceName $serviceName
# Example: .\Restart-WindowsService.ps1 -ServiceName W3SVC
# Notes: Works with either the service name OR the display name.
param(
[string] $ServiceName
)
Import-Module -Name PSSlack
# Force use of TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Setup variables
$scriptPath = "$($PSScriptRoot)\$($MyInvocation.MyCommand)"
$textWarning = "Warning"
$textError = "ERROR"
$messages = @()
$service = $null
$slackText = $null
# Custom variables
$secondsToWait = 10
$ip = $(Get-NetIPAddress -InterfaceIndex 12).IPAddress # Set IP address automatically
#$ip = "FI.LL.ME.IN" # Set IP address manually
$slackToken = "CHANGEME"
$slackChannel = "#your-slack-channel"
# Add lines of text in the slack message
$messages += "*Service Restart - $($ServiceName)*"
$messages += "*Server*: $($env:ComputerName) - $($ip)"
$messages += "*Script*: $($scriptPath)"
# Get the service's info
$service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($service) {
if ($service.Status -ne "Running") {
$messages += "*$($textWarning)*: Service was not running. Starting and waiting $secondsToWait seconds."
Start-Service -Name $ServiceName
Start-Sleep -Seconds $secondsToWait
}
Restart-Service -Name $ServiceName
Start-Sleep -Seconds $secondsToWait
$service = Get-Service -Name $ServiceName
if ($service.Status -eq "Running") {
$messages += "Service restarted successfully."
}
else {
$messages += "*$($textError): Unable to restart the service. Please restart it manually.*"
}
}
else {
$messages += "*$($textError): Service not found*"
}
# Combine messages and send slack notification to channel
$slackText = $messages -join "`n"
Send-SlackMessage -Token $slackToken -Channel $slackChannel -Text $slackText -AsUser
Write-Output $slackText