-
Notifications
You must be signed in to change notification settings - Fork 0
/
Restart-Servers-Cmdlet.ps1
146 lines (129 loc) · 5.33 KB
/
Restart-Servers-Cmdlet.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<#
.Synopsis
This function will restart the machines/servers provided as input to the cmdlet.
.Description
This function will restart the machines/servers.
.Parameter Servers
This parameter is the list of machines/servers to restart.
This parameter is mandatory, possible inputs is the comma separated list of machine/server names.
.Parameter Username
This parameter is used to set the username having access to the machines/servers.
This parameter is mandatory, possible input is the username having admin access to the machines/servers.
.Parameter Password
This parameter is used to set the password to the username provided for the machines/servers.
This parameter is mandatory, possible input is the password to the username having admin access for the machines/servers.
.Parameter WaitForServerToRestart
This parameter is used to set the time to wait for server to restart.
This parameter is not mandatory, possible input is time in seconds. Default value is 60 seconds.
.Parameter PingRetries
This parameter is used to set the number of ping retries to check the machine is online again.
This parameter is not mandatory, possible input is the number of times a ping is required. Default value is 4.
.Example
Restart-Servers -Servers "PC-A" -Username "domain\abc" -Password "xyz"
This command is used to restart a Machine/Server
.Example
Restart-Servers -Servers "PC-A,PC-B,PC-C,PC-D" -Username "domain\abc" -Password "xyz"
This command is used to restart Multiple Machines/Servers
.Example
Restart-Servers -Servers "PC-A" -Username "domain\abc" -Password "xyz" -WaitForServerToRestart "120" -PingRetries "2"
This command is used to restart a Machine/Server, wait And try pinging the Machine/Server
.Example
Restart-Servers -Servers "PC-A,PC-B,PC-C,PC-D" -Username "domain\abc" -Password "xyz" -WaitForServerToRestart "60" -PingRetries "8"
This command is used to restart Multiple Machines/Servers, wait And try pinging the Machines/Servers
#>
Function Restart-Servers
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$Servers,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$Username,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$Password,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$WaitForServerToRestart,
[Parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$PingRetries
)
Begin
{
Function Write-Log([string]$logMessage)
{
$print = $logMessage.length + 6
Write-Output $("-" * ($print))
Write-Output "[[ $logMessage ]]"
write-output $("-" * ($print))
}
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
$Servers = $Servers.Split(',')
[string]$ServerName
$ServersRestarted = New-Object System.Collections.Generic.List[System.Object]
if($WaitForServerToRestart -eq "")
{
$WaitForServerToRestart = "60"
}
if($PingRetries -eq "")
{
$PingRetries = "4"
}
}
Process
{
try
{
foreach ($Server in $Servers)
{
Restart-Computer -ComputerName $Server -Credential $Credentials -Force -ErrorAction SilentlyContinue
if($? -eq $false)
{
$ServerName = $Server
Write-Log("Restart-Servers ERROR on Machine :: '$ServerName', couldn't restart this machine. Check machine name or credentials and try again.")
}
else
{
$ServersRestarted.Add($Server)
}
}
if($ServersRestarted.Count -ne 0)
{
Start-Sleep -s $WaitForServerToRestart
}
foreach($Server in $ServersRestarted)
{
for($i=1; $i -le $PingRetries; $i++)
{
$a = Test-Connection -ComputerName "$Server" -count 1 -Quiet
if($a -eq $true)
{
$ServerName = $Server
}
else
{
Start-Sleep -Seconds 50
}
}
if($a -eq $true)
{
Write-Log("Machine - '$ServerName' restarted and is up and running.")
}
else
{
Write-Log("Machine - '$ServerName' restarted and but is not responding.")
}
}
}
catch
{
Write-Log("ERROR on Machine :: '$ServerName'")
Write-Log("Error Message :: $_.Exception.Message")
Break
}
}
End
{
Write-Log(":::: 'Restart-Servers' executed successfully. ::::")
}
}
<# Restart-Servers -Servers "PC1,PC2,PC3,PC4" -Username "domain\<username>" -Password "<password>" -WaitForServerToRestart "60" -PingRetries "4" #>