-
Notifications
You must be signed in to change notification settings - Fork 28
/
Updater.ps1
117 lines (98 loc) · 4.1 KB
/
Updater.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
########################################################
# IT Admin Toolkit Auto Updater #
# Date: 3/16/2022 #
# Written by: Nathan Kasco #
########################################################
param(
[string]
$InstallPath,
[string]
$DownloadURL
)
#Info: This script assumes the check for updates has already been done, therefore we just need to download/extract the update and then move the files into place.
if(!($InstallPath) -or !($DownloadURL)){
Exit 1 #TODO: Make this more meaningful
}
try{
Invoke-WebRequest -Uri $DownloadURL -OutFile "$Env:Temp\ITATKLatest.zip" -UseBasicParsing -ErrorAction Stop
} catch {
Write-Host "Error downloading update $_"
exit 1 #TODO: Make this more meaningful
}
Start-Sleep -Seconds 2
#Extract the update
try{
$TempZipPath = "$Env:Temp\ITATKLatest.zip"
$TempPath = "$Env:Temp\ITATKLatest"
if(Test-Path $TempZipPath){
Expand-Archive -Path $TempZipPath -DestinationPath $TempPath -ErrorAction Stop
} else {
Exit 1 #TODO: Make this more meaningful
}
} catch {
#TODO: Handle errors
}
#Don't start until the main UI is closed to prevent any file in use errors
do{
$ProcessCheck = $null
$ProcessCheck = Get-Process ITATKWinUI -ErrorAction SilentlyContinue
if($ProcessCheck){
$ProcessCheck | Stop-Process -Force -ErrorAction SilentlyContinue
}
start-sleep -seconds 3
} while ($ProcessCheck)
if(Test-Path $TempPath){
#Compare the current version to the new version
$TempFiles = Get-ChildItem -Path $TempPath
$InstallFiles = Get-ChildItem -Path $InstallPath
#Delete current files
foreach($TempFile in $InstallFiles){
if($TempFile.Name -notin "Settings.xml","XML","Scripts","Updater.ps1"){
Remove-Item -Path $TempFile.FullName -Force -Recurse
}
}
#Move the files into place, ensure we skip user files
foreach($TempFile in $TempFiles){
if($TempFile.Name -notin "Settings.xml","XML","Scripts","Updater.ps1"){
Move-Item -Path $TempFile.FullName -Destination $InstallPath -Force
}
}
#Handle Settings.xml file
#Checks for new config item in "settings" node of `Settings.xml` and append new settings while preserving existing settings.
$CurrentSettingsPath = "$InstallPath\Settings.xml"
$NewSettingsPath = "$Env:Temp\ITATKLatest\Settings.xml"
if (Test-Path $CurrentSettingsPath) {
try{
$CurrentXML = [xml](Get-Content $CurrentSettingsPath -ErrorAction Stop)
$NewSettingsXML = [xml](Get-Content $NewSettingsPath -ErrorAction Stop)
#Compare and find new settings
$NewSettingsNames = Compare-Object $CurrentXML.Settings.ChildNodes.Name $NewSettingsXML.Settings.ChildNodes.Name -ErrorAction Stop | Where-Object {($_.SideIndicator -eq '=>')} -ErrorAction Stop | Select-Object -ExpandProperty InputObject -ErrorAction Stop
if ($NewSettingsNames) {
foreach ($NewSetting in $NewSettingsNames) {
$config = (Get-Content $NewSettingsPath -ErrorAction Stop | Select-String -Pattern $NewSetting ).ToString()
$CurrentXML.Settings.InnerXml += $config.Trim()
}
$CurrentXML.Save($CurrentSettingsPath)
}
} catch {
#TODO: Handle errors - This will get updated when the updater itself is overhauled
}
}
#Optimization
<#
$CurrentFiles = Get-ChildItem -Path "$InstallPath" -Recurse
$Differences = Compare-Object -ReferenceObject $CurrentFiles -DifferenceObject $TempFiles
foreach($Difference in $Differences){
try{
Move-Item -Path $Difference -Destination $InstallPath -Force -ErrorAction Stop
} catch {
#TODO: Handle errors
}
}#>
#Cleanup
Remove-Item -Path "$Env:Temp\ITATKLatest.zip" -Force -ErrorAction Stop
Remove-Item -Path $TempPath -Recurse -Force -ErrorAction Stop
Start-Process -FilePath "$InstallPath\ITATKWinUI.exe"
} else {
Exit 1 #TODO: Make this more meaningful
}