-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitsc_checks.ps1
263 lines (195 loc) · 8.27 KB
/
itsc_checks.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
Param(
[Parameter(Mandatory=$True,HelpMessage='Enter the computer name or IP')]
[String]$Computer,
[Parameter(Mandatory=$True,HelpMessage='Enter your Username')]
[String]$Username,
[Parameter(Mandatory=$True,HelpMessage='Enter your Password')]
[SecureString]$Password,
[Parameter(HelpMessage='Defines if we run in interactive mode or not')]
[Bool]$Interactive = $true
)
# Define ISC Servers
[Array] $ISCservers = '172.31.255.17', '172.31.255.6'
# Define ports on remote server that should be open
[Array] $TestPorts = '139', '445'
# Assemble Credential for remote connection
$cred = New-Object System.Management.Automation.PSCredential ($Username, $Password)
# -----------------------------------------------------------
function StartEnable-Service
{
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$ServiceName,
[Parameter(HelpMessage='Defines if we run in interactive mode or not')]
[Bool]$Interactive = $false
)
$error.clear()
$arrService = Get-Service -Name $ServiceName
if ($error) {
Write-Host "Error Occured"
Return $error
}
if ($Interactive -eq $true -And $arrService.Status -ne 'Running') {
$AskStartServiceTitle = 'Start Service'
$AskStartServiceQuestion = "$ServiceName service is not running. Would you like to start it?"
$AskStartServiceChoices = '&Yes', '&No'
$AskStartServiceDecision = $Host.UI.PromptForChoice($AskStartServiceTitle, $AskStartServiceQuestion, $AskStartServiceChoices, 1)
if ($AskStartServiceDecision -eq 0) {
$AnswerStartService = $true
}
else {
$AnswerStartService = $false
}
}
elseif ($Interactive -eq $true -And $arrService.Status -eq 'Running') {
Write-Host "$ServiceName service is already running"
}
if ($Interactive -eq $true -And $arrService.StartType -ne 'Automatic') {
$AskEnableServiceTitle = 'Load service on Startup'
$AskEnableServiceQuestion = "$ServiceName service is not set to run on Startup. Would you like to enable it?"
$AskEnableServiceChoices = '&Yes', '&No'
$AskEnableServiceDecision = $Host.UI.PromptForChoice($AskEnableServiceTitle, $AskEnableServiceQuestion, $AskEnableServiceChoices, 1)
if ($AskEnableServiceDecision -eq 0) {
$AnswerEnableService = $true
}
else {
$AnswerEnableService = $false
}
}
elseif ($Interactive -eq $true -And $arrService.StartType -ne 'Automatic') {
Write-Host "$ServiceName service is already set to automatically start"
}
if ($Interactive -eq $false -Or $AnswerStartService -eq $true ) {
Write-Host "Checking Service: $ServiceName"
while ($arrService.Status -ne 'Running')
{
Start-Service $ServiceName
Write-Host $arrService.status
Write-Host 'Service starting'
Start-Sleep -seconds 60
$arrService.Refresh()
if ($arrService.Status -ne 'Running') {
Write-Host "ERROR: Unable to start $ServiceName Service"
}
}
}
if ($Interactive -eq $false -Or $AnswerEnableService -eq $true ) {
if ($arrService.StartType -ne 'Automatic') {
Set-Service -Name $ServiceName -StartupType Automatic
Write-Host "$ServiceName has been set to Automatic startup"
}
else
{
Write-Host "$ServiceName is already set to Automatic startup - no action required"
}
}
}
# -----------------------------------------------------------
Write-Host ''
Write-Host "Connection Test - Pre-checks"
Write-Host "-------------------------------------------------------"
# Pre-Checks
$WinRMTest = Test-NetConnection -ComputerName $Computer -Port 5985 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
Write-Host "Testing WinRM connection (port 5985) from local machine to target $Computer"
if ($WinRMTest.TcpTestSucceeded -ne $true) {
Write-Host "WARNING: WinRM Connection to $Computer is unavailable"
Break
}
else {
Write-Host "SUCCESS: WinRM Connection to $Computer on port 5985 available"
}
Write-Host ''
# Test connections to target machine
Write-Host "Testing ports on $Computer from local machine"
foreach($Port in $TestPorts) {
Test-NetConnection -ComputerName $Computer -Port $Port -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | select -Property RemotePort, TcpTestSucceeded
}
Write-Host ''
# -----------------------------------------------------------
# Remote Checks
Write-Host "Testing connections from $Computer to ISC Servers"
# Reinitalise all variables
$CheckISCServer = $null
$CheckISCSuccess = $false
$CheckISCTarget = $null
foreach($Server in $ISCservers) {
$CheckISCServer = Invoke-Command -ComputerName $Computer -Credential $cred -ScriptBlock {
$CheckISCServer = Test-NetConnection -ComputerName $Using:Server -Port 3121 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | select -Property RemoteAddress, RemotePort, TcpTestSucceeded
Return $CheckISCServer
}
if ($CheckISCServer.TcpTestSucceeded -eq $true) {
$CheckISCSuccess = $true
$CheckISCTarget = $CheckISCServer.RemoteAddress
}
}
if ($CheckISCSuccess -eq $false) {
Write-Host "WARNING: No connection available to any UKCloud ISC Server"
Break
}
else {
Write-Host "SUCCESS: Connection to UKCloud ISC server available: $CheckISCTarget"
}
Write-Host ''
$FWRfpsStatus = Invoke-Command -ComputerName $Computer -Credential $cred -ScriptBlock {
$FWRfpsiStatus = Get-NetFirewallRule -DisplayName "File and Printer Sharing (SMB-In)" | select -Property Enabled
Return $FWRfpsiStatus
}
# -----------------------------------------------------------
# Interactive
if ($Interactive -eq $true) {
Write-Host "Running in interactive mode"
Write-Host ''
Write-Host 'Checking Firewall'
Write-Host "-------------------------------------------------------"
if ($FWRfpsStatus.Enabled -ne $true) {
$FWRfpsTitle = 'Firewall Rule'
$FWRfpsQuestion = "Firewall rule on $Computer is not enabled. Would you like to enable it?"
$FWRfpsChoices = '&Yes', '&No'
$FWRfpsDecision = $Host.UI.PromptForChoice($FWRfpsTitle, $FWRfpsQuestion, $FWRfpsChoices, 1)
if ($FWRfpsDecision -eq 0) {
Invoke-Command -ComputerName $Computer -Credential $cred -ScriptBlock {
Enable-NetFirewallRule -DisplayName "File and Printer Sharing (SMB-In)"
}
Write-Host "Firewall rule for File and Printer Sharing (SMB-In) on $Computer has been enabled."
} else {
Write-Host "Firewall rule for File and Printer Sharing (SMB-In) on $Computer not enabled."
}
} else {
Write-Host "Firewall rule for File and Printer Sharing (SMB-In) on $Computer already enabled. No action required"
}
}
# Non-Interactive
if ($Interactive -eq $false) {
Write-Host "Running in non-interactive mode"
Write-Host ''
Write-Host 'Checking Firewall'
Write-Host "-------------------------------------------------------"
if ($FWRfpsStatus.Enabled -ne $true) {
Invoke-Command -ComputerName $Computer -Credential $cred -ScriptBlock {
Enable-NetFirewallRule -DisplayName "File and Printer Sharing (SMB-In)"
}
Write-Host "Firewall rule for File and Printer Sharing (SMB-In) on $Computer not enabled."
} else {
Write-Host "Firewall rule for File and Printer Sharing (SMB-In) on $Computer already enabled. No action required"
}
}
# -----------------------------------------------------------
Write-Host ''
Write-Host 'Checking Services'
Write-Host "-------------------------------------------------------"
Invoke-Command -ComputerName localhost -Credential $cred -ScriptBlock ${Function:StartEnable-Service} -ArgumentList 'LanmanServer', $Interactive
Invoke-Command -ComputerName localhost -Credential $cred -ScriptBlock ${Function:StartEnable-Service} -ArgumentList 'RemoteRegistry', $Interactive
# -----------------------------------------------------------
Write-Host ''
# Test connections to target machine
Write-Host ''
Write-Host "Connection Test - Post-Checks"
Write-Host "-------------------------------------------------------"
Write-Host ''
Write-Host "RemotePort TcpTestSucceeded"
Write-Host "---------- ---------------- "
foreach($Port in $TestPorts)
{
Test-NetConnection -ComputerName $Computer -Port $Port -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | select -Property RemotePort, TcpTestSucceeded
}