-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindows heathly check.ps1
291 lines (233 loc) · 8.25 KB
/
Windows heathly check.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#requires -version 2
<#
.SYNOPSIS
A script to restart automatic services on remote VMs and send an email report.
.DESCRIPTION
This script imports a CSV file that contains a list of VM names and another CSV file that contains AD credentials.
It loops through each VM name and checks if it has any stopped automatic services.
If yes, it restarts the services and collects the status results.
If no, it collects the status results.
It outputs the service status for each VM to an HTML file with a beach background and a dragon, sword and phoenix frame.
It sends an email with the HTML file and the log file as attachments to a specified address.
.PARAMETER VMList
The path to the CSV file that contains the VM names.
.PARAMETER CredentialList
The path to the CSV file that contains the AD credentials.
.PARAMETER EmailList
The path to the CSV file that contains the email addresses.
.INPUTS
None
.OUTPUTS
An HTML file stored in C:\Windows\Temp\VMReport.html
A log file stored in C:\Windows\Temp\RestartServices.log
.NOTES
Version: 1.0
Author: Bing
Creation Date: 15/04/2023
Purpose/Change: Initial script development
.EXAMPLE
.\RestartServices.ps1 -VMList C:\VMs.csv -CredentialList C:\Credentials.csv -EmailList C:\Emails.csv
#>
#--------------------------------------------------------- [Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"
#Dot Source required Function Libraries
. "C:\Scripts\Functions\Logging_Functions.ps1"
#---------------------------------------------------------- [Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = "1.0"
#Log File Info
$sLogPath = "C:\Windows\Temp"
$sLogName = "RestartServices.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#HTML File Info
$sHTMLPath = "C:\Windows\Temp"
$sHTMLName = "VMReport.html"
$sHTMLFile = Join-Path -Path $sHTMLPath -ChildPath $sHTMLName
#Email Info
$sSMTPServer = "smtp.example.com"
$sSubject = "VM Service Status Report"
$sBody = "Please find attached the VM service status report and the log file generated by PowerShell script."
#----------------------------------------------------------- [Functions]------------------------------------------------------------
<#
Function Get-VMServiceStatus {
Param (
[Parameter(Mandatory=$true)]
[string]$VMName,
[Parameter(Mandatory=$true)]
[pscredential]$Credential
)
Begin {
Log-Write -LogPath $sLogFile -LineValue "Getting service status for $VMName..."
}
Process {
Try {
#Get all automatic services on the remote VM
$Services = Get-Service -ComputerName $VMName -Credential $Credential | Where-Object {$_.StartType -eq "Automatic"}
#Check if any service is stopped
If ($Services.Status -contains "Stopped") {
#Restart the stopped services
Log-Write -LogPath $sLogFile -LineValue "Restarting stopped services on $VMName..."
$Services | Where-Object {$_.Status -eq "Stopped"} | Restart-Service -PassThru
#Wait for 10 seconds to allow the services to start
Start-Sleep -Seconds 10
#Refresh the service status
$Services.Refresh()
}
#Return the service status as an object array
Return $Services | Select-Object Name, DisplayName, Status
}
Catch {
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $False
Break
}
}
End {
If ($?) {
Log-Write -LogPath $sLogFile -LineValue "Completed getting service status for $VMName."
Log-Write -LogPath $sLogFile -LineValue ""
}
}
}
Function ConvertTo-HTMLReport {
Param (
[Parameter(Mandatory=$true)]
[object[]]$ServiceStatus,
[Parameter(Mandatory=$true)]
[string]$VMName,
[Parameter(Mandatory=$true)]
[string]$HTMLFile
)
Begin {
Log-Write -LogPath $sLogFile -LineValue "Converting service status to HTML report for $VMName..."
}
Process {
Try {
#Create a HTML header with a beach background and a dragon, sword and phoenix frame
$HTMLHeader = @"
<html>
<head>
<style>
body {
background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e');
background-size: cover;
background-repeat: no-repeat;
}
table {
border: 5px solid black;
border-collapse: collapse;
margin: auto;
width: 80%;
}
th, td {
border: 1px solid black;
padding: 10px;
text-align: center;
}
th {
background-color: lightblue;
color: black;
}
td {
background-color: white;
color: black;
}
h1 {
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<h1>VM Service Status Report</h1>
<img src="https://images-na.ssl-images-amazon.com/images/I/61fj0Y%2B4ZKL._AC_SX466_.jpg" alt="Dragon, Sword and Phoenix" width="100%" height="300">
"@
#Create a HTML table with the service status
$HTMLTable = $ServiceStatus | ConvertTo-Html -Fragment -As Table -PreContent "<h2>$VMName</h2>"
#Create a HTML footer
$HTMLFooter = @"
</body>
</html>
"@
#Combine the HTML header, table and footer
$HTMLReport = $HTMLHeader + $HTMLTable + $HTMLFooter
#Write the HTML report to the file
$HTMLReport | Out-File -FilePath $HTMLFile -Append
}
Catch {
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $False
Break
}
}
End {
If ($?) {
Log-Write -LogPath $sLogFile -LineValue "Completed converting service status to HTML report for $VMName."
Log-Write -LogPath $sLogFile -LineValue ""
}
}
}
Function Send-EmailReport {
Param (
[Parameter(Mandatory=$true)]
[string[]]$EmailList,
[Parameter(Mandatory=$true)]
[string]$SMTPServer,
[Parameter(Mandatory=$true)]
[string]$Subject,
[Parameter(Mandatory=$true)]
[string]$Body,
[Parameter(Mandatory=$true)]
[string]$HTMLAttachment,
[Parameter(Mandatory=$true)]
[string]$LogAttachment
)
Begin {
Log-Write -LogPath $sLogFile -LineValue "Sending email report to $($EmailList -join ', ')..."
}
Process {
Try {
#Send an email with the attachments to the email list
Send-MailMessage -To $EmailList -From "[email protected]" -SmtpServer $SMTPServer -Subject $Subject -Body $Body -Attachments $HTMLAttachment, $LogAttachment
}
Catch {
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $False
Break
}
}
End {
If ($?) {
Log-Write -LogPath $sLogFile -LineValue "Completed sending email report to $($EmailList -join ', ')."
Log-Write -LogPath $sLogFile -LineValue ""
}
}
}
#----------------------------------------------------------- [Execution]------------------------------------------------------------
#Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
#Script Execution goes here
#Parse the parameters
Param (
[Parameter(Mandatory=$true)]
[string]$VMList,
[Parameter(Mandatory=$true)]
[string]$CredentialList,
[Parameter(Mandatory=$true)]
[string]$EmailList
)
#Import the CSV files
$VMNames = Import-Csv -Path $VMList
$Credentials = Import-Csv -Path $CredentialList
$EmailAddresses = Import-Csv -Path $EmailList
#Loop through each VM name
ForEach ($VM in $VMNames) {
#Get the corresponding credential from the credential list
$Credential = New-Object pscredential($Credentials.UserName, (ConvertTo-SecureString $Credentials.Password –AsPlainText –Force))
#Get the service status for the VM
$ServiceStatus = Get-VMServiceStatus -VMName $VM.Name –Credential $Credential
#Convert the service status to HTML report and append to the HTML file
ConvertTo-HTMLReport -ServiceStatus $ServiceStatus -VMName $VM.Name -HTMLFile $sHTMLFile
}
#Send the email report with the HTML file and the log file as attachments to the email list
Send-EmailReport -EmailList $EmailAddresses.Email -SMTPServer $sSMTPServer -Subject $sSubject -Body $sBody -HTMLAttachment $sHTMLFile -LogAttachment $sLogFile
#Log-Finish -LogPath $sLogFile
```