forked from nkasco/IT-Admin-Toolkit-WinUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.ps1
91 lines (77 loc) · 2.8 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
########################################################
# 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
}
}
#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
}