-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHearth.ps1
More file actions
242 lines (214 loc) · 8.51 KB
/
Hearth.ps1
File metadata and controls
242 lines (214 loc) · 8.51 KB
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
# Requires -RunAsAdministrator
# Requires -Version 5.1
<#
.SYNOPSIS
Hearth - A comprehensive system maintenance utility for Windows.
.DESCRIPTION
Automates common system maintenance tasks including package updates,
system cleanup, security checks, and Windows updates. Handles first-time
installation and repair seamlessly.
.NOTES
File Name : Hearth.ps1
Author : Kaleb Weise (A *Mute Observer)
Version : 1.0.0
#>
# ASCII Art Logo
$logo = @'
/\ ╔══════════════════════════════════╗
/ \ ║ Welcome to Hearth ║
/____\ ║ -------------------------------- ║
/| |\ _____ ║ Automated System Maintenance ║
/_|____| \ ( ) ║ for Windows ║
| | \ / ║ ║
| []| \ / ║ Version: 1.0.0 ║
|____|_______V______ ║ Author: Kaleb Weise ║
| | _ | | ║ (A *Mute Observer) ║
| | (_) | [] | ║ ║
|____|______ |______| ║ "Keeping your system kindled ║
/|\ /|\ ║ and running smoothly." ║
/ | \ / | \ ║ ║
/ | \/ | \ ║ Press any key to begin... ║
/___|_______|___\ ╚══════════════════════════════════╝
| | | | |
^ ^ ^ ^ ^
'@
# Determine script directory
$scriptDirectory = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Path $MyInvocation.MyCommand.Path -Parent }
# Paths and Variables
$hearthDirectory = Join-Path -Path $env:LOCALAPPDATA -ChildPath 'Hearth'
$configDirectory = Join-Path -Path $hearthDirectory -ChildPath 'Config'
$configFile = Join-Path -Path $configDirectory -ChildPath 'HearthSettings.json'
$schemaFile = Join-Path -Path $configDirectory -ChildPath 'HearthSettings.schema.json'
$modulesDirectory = Join-Path -Path $scriptDirectory -ChildPath 'Modules'
# Function to initialize directories and configuration files
function Initialize-Hearth {
try {
# Create directories if they don't exist
if (-not (Test-Path -Path $configDirectory -PathType Container)) {
$null = New-Item -Path $configDirectory -ItemType Directory -Force
Write-Output "Created directory: $configDirectory"
}
# Create initial configuration file if it doesn't exist
if (-not (Test-Path -Path $configFile -PathType Leaf)) {
$initialConfig = @{
AutomatedUpdates = $true
SystemMaintenance = $true
UpdateChocolatey = $true
InvokeSFCScan = $true
OverrideUpdates = $false
OverrideMaintenance = $false
} | ConvertTo-Json -Depth 4
$initialConfig | Set-Content -Path $configFile -Force
Write-Output "Created initial configuration file: $configFile"
}
# Create schema file if it doesn't exist or doesn't match the expected schema
$expectedSchema = @{
"$schemaContent" = @{
"type" = "object"
"properties" = @{
"AutomatedUpdates" = @{
"type" = "boolean"
}
"SystemMaintenance" = @{
"type" = "boolean"
}
"UpdateChocolatey" = @{
"type" = "boolean"
}
"InvokeSFCScan" = @{
"type" = "boolean"
}
"OverrideUpdates" = @{
"type" = "boolean"
}
"OverrideMaintenance" = @{
"type" = "boolean"
}
}
"required" = @(
"AutomatedUpdates",
"SystemMaintenance",
"UpdateChocolatey",
"InvokeSFCScan",
"OverrideUpdates",
"OverrideMaintenance"
)
"additionalProperties" = $false
}
} | ConvertTo-Json -Depth 4
$currentSchema = $null
if (Test-Path -Path $schemaFile -PathType Leaf) {
$currentSchema = Get-Content -Path $schemaFile -Raw | ConvertFrom-Json
}
if (-not $currentSchema -or ($currentSchema | ConvertTo-Json -Depth 4) -ne ($expectedSchema | ConvertTo-Json -Depth 4)) {
$expectedSchema | Set-Content -Path $schemaFile -Force
Write-Output "Created or updated schema file: $schemaFile"
}
Write-Output "Directories initialized successfully."
}
catch {
Write-Error "Initialization failed: $_"
Start-Sleep -s 10
exit 1
}
}
# Function to load configuration and validate using JSON Schema
function Load-Configuration {
param (
[Parameter(Mandatory = $true)]
[string]$ConfigPath,
[Parameter(Mandatory = $true)]
[string]$SchemaPath
)
try {
$config = Get-Content -Path $ConfigPath -Raw -ErrorAction Stop | ConvertFrom-Json
$schema = Get-Content -Path $SchemaPath -Raw -ErrorAction Stop | ConvertFrom-Json
if ($config -isnot [object]) {
throw "Invalid configuration format. Expected JSON object."
}
# Perform validation against schema
$isValid = $config.PSObject.Properties.Name -eq $schema.PSObject.Properties.Name
if (-not $isValid) {
throw "Configuration does not match the schema."
}
Write-Output "Configuration loaded and validated successfully."
return $config
}
catch {
Write-Error "Failed to load or validate configuration: $_"
exit 1
}
}
# Function to import Hearth modules
function Import-HearthModule {
param (
[Parameter(Mandatory = $true)]
[string]$ModuleName
)
try {
if (Test-Path $ModuleName) {
Import-Module $ModuleName -ErrorAction Stop
Write-Output "Module imported successfully: $ModuleName"
} else {
throw "$ModuleName module not found. Please ensure it is installed."
}
}
catch {
Write-Error "Failed to import module: $_"
exit 1
}
}
# Function to execute Hearth tasks
function Execute-Hearth {
try {
# Perform automated updates if enabled
if ($config.AutomatedUpdates) {
Invoke-ChocolateyUpdate
Invoke-ScoopUpdate
Invoke-WingetUpdate
}
# Perform system maintenance if enabled
if ($config.SystemMaintenance) {
Invoke-SFCScan
Invoke-DefenderScan
Invoke-DiskCleanup
}
Write-Output "Hearth tasks executed successfully."
}
catch {
Write-Error "Failed to execute Hearth tasks: $_"
exit 1
}
}
# Main script logic
try {
Clear-Host
Write-Output $logo
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
Clear-Host
Initialize-Hearth
Write-Output "Hearth initialized successfully."
# Load configuration and validate
$config = Load-Configuration -ConfigPath $configFile -SchemaPath $schemaFile
# Import Hearth modules
$Modules = @(
"$modulesDirectory\HearthInstaller\HearthInstaller.psm1",
"$modulesDirectory\Invoke-ChocolateyUpdate\Invoke-ChocolateyUpdate.psm1",
"$modulesDirectory\Invoke-ScoopUpdate\Invoke-ScoopUpdate.psm1",
"$modulesDirectory\Invoke-WingetUpdate\Invoke-WingetUpdate.psm1",
"$modulesDirectory\Invoke-SFCScan\Invoke-SFCScan.psm1",
"$modulesDirectory\Invoke-DefenderScan\Invoke-DefenderScan.psm1",
"$modulesDirectory\Invoke-DiskCleanup\Invoke-DiskCleanup.psm1"
)
$Modules | ForEach-Object {
Import-HearthModule -ModuleName $_
}
# Execute Hearth tasks
Execute-Hearth
Write-Host "Script finished executing"
Read-Host "Press Enter to continue..."
}
catch {
Write-Error "An unexpected error occurred: $_"
exit 1
}