Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
DXFH authored Apr 15, 2023
1 parent e238314 commit e49e634
Showing 1 changed file with 291 additions and 0 deletions.
291 changes: 291 additions & 0 deletions Windows heathly check.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
```

0 comments on commit e49e634

Please sign in to comment.