-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestartServiceOnMultipleComputers.ps1
65 lines (50 loc) · 1.75 KB
/
RestartServiceOnMultipleComputers.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
# RESTART SERVICE ON MULTIPLE COMPUTERS
#
# Takes ServiceName, Computer list, login credential
#
# Script tries to log in to every computer in the computer list using the given Credential
# It then tries to restart the service using Invoke-Command
#
# Optionally, you can execute the bottom foreach to first check when the services were started
# (and thus if they need restarting at all)
# Config
$ServiceName = 'SplunkForwarder'
$Computers = @('compname1','compname2','compname3','compname4')
$Credential = Get-Credential
# Restart service on all computers
foreach ($computer in $computers)
{
Write-Host "`n" $computer
# Test Powershell Remoting
$res = Test-WsMan $computer -ErrorAction SilentlyContinue
# No error connecting
if ($res)
{
# Restart service
Invoke-Command -ComputerName $computer -ScriptBlock { Restart-Service -Name $args[0] } -credential $Credential -ArgumentList $ServiceName
}
# Error connecting
else
{
Write-Host "Could not connect to " $computer -ForegroundColor Red
}
}
# [Optional] Check time when the service was start.
foreach ($computer in $computers)
{
Write-Host "`n" $computer -ForegroundColor Cyan
# Test Powershell Remoting
$res = Test-WsMan $computer -ErrorAction SilentlyContinue
# No error connecting
if ($res)
{
Invoke-Command -ComputerName $computer -credential $Credential -ArgumentList $ServiceName -ScriptBlock{
(Get-EventLog -LogName “System” -Source “Service Control Manager” -EntryType “Information” -Message (“*"+$args[0]+"*running*”) -Newest 1).TimeGenerated
}
}
# Error connecting
else
{
write-host "Could not connect to " $computer -ForegroundColor Red
}
}