-
Notifications
You must be signed in to change notification settings - Fork 2
/
Start-AutoUpdate.ps1
50 lines (43 loc) · 2 KB
/
Start-AutoUpdate.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
<#
.SYNOPSIS
Allows for a script to check for a newer version of itself. If the File has been updated then this will update it and exit.
This should be the first thing the script does.
.PARAMETER Updateserver_UNC_Path
The folder location of the master copy of the script
Eg. \\server\share\
.PARAMETER Scriptname
The file name of the script
EG. executeorder66.ps1
.EXAMPLE
Start-AutoUpdate -updateserver_UNC_Path "\\server\User Creation" -scriptname "UserCreation.ps1"
.NOTES
Daryl Bizsley 2015
#>
#Messagebox Function
Function New-MSGBox ($message){
#Load .Net Assembly for message box
[Reflection.Assembly]::LoadFile("C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll") | out-null
[System.Windows.Forms.MessageBox]::Show("$message")
}
#Declare AutoUpdate Function
Function Start-AutoUpdate([String]$updateserver_UNC_Path, [String]$scriptname) {
#Declare Variables
$updateserver_UNC_Path = $updateserver_UNC_Path
$scriptname = $scriptname
$currentdir = (Get-Location).Path
#Check If Hash matches to detect change this requires powershell 3.0
$master = (Get-fileHash "$updateserver_UNC_Path\$scriptname").Hash
$current = (Get-fileHash "$currentdir\$scriptname").Hash
IF ($current -ne $master){
#Set Update Script path as something powershell will execute if spaces are in the name
$updatescript = "&('$currentdir\updater.ps1')"
#Get a copy of the latest script and download it, ready to apply after this script exits.
Copy-Item "$updateserver_UNC_Path\$scriptname" "$currentdir\$scriptname.update" -confirm:$false
#Generate the script which will overwrite this one with the latest
echo "move-item '$currentdir\$scriptname.update' '$currentdir\$scriptname' -Force" | out-file "$currentdir\updater.ps1"
New-MSGBox "An Update to this script will now be applied. This Script will now Exit Please Re-Run."
#Call the update script before killing this one.
powershell.exe $updatescript
Exit
}
}