-
Notifications
You must be signed in to change notification settings - Fork 3
/
EmbyServiceUpdater.ps1
175 lines (159 loc) · 7 KB
/
EmbyServiceUpdater.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
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
param([switch]$InstallTask, [switch]$UninstallTask, [switch]$UpdateScript, [string]$ApiKey, [string]$ServerUrl)
#requires -runasadministrator
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Class EmbyServiceUpdater {
[string]$location
[object]$release
[string]$assetUrl
[string]$serviceName
[string]$serverUrl
[string]$apiKey
[object]$serverInfo
EmbyServiceUpdater([string]$serverUrl, [string]$apiKey) {
$this.serverUrl = $serverUrl
$this.apiKey = $apiKey
$this.GetLocation()
}
GetServiceName() {
$this.serviceName = (Get-Service | Where-Object { $_.name -match "emby" } | Select-Object -first 1).name
if ($this.serviceName.Length -eq 0) {
throw "can not find emby service"
}
}
GetServerInfo() {
$this.serverInfo = Invoke-RestMethod "$($this.serverUrl)/emby/System/Info?api_key=$($this.apiKey)"
}
GetLatestRelease() {
$releases = Invoke-RestMethod "https://api.github.com/repos/mediabrowser/Emby.Releases/releases" -UseBasicParsing
$preRelease = $this.serverInfo.SystemUpdateLevel -ne "Release"
$this.release = ($releases | Where-Object { $_.prerelease -eq $preRelease } | Select-Object -first 1)
}
GetLocation() {
$this.location = (Get-Item (Get-Process embyserver | Select-Object -first 1).Path).Directory.Parent.FullName
}
GetAssetUrl() {
if ([Environment]::Is64BitOperatingSystem) {
$this.assetUrl = ($this.release.assets | Where-Object { $_.name -match "embyserver-win-x64" } | Select-Object -first 1).browser_download_url
}
else {
$this.assetUrl = ($this.release.assets | Where-Object { $_.name -match "embyserver-win-x86" } | Select-Object -first 1).browser_download_url
}
}
GetUpdate() {
$this.GetServiceName()
$this.GetServerInfo()
$this.GetLatestRelease()
try {
if ([Version]$this.release.tag_name -gt [Version]$this.serverInfo.Version) {
if (-not (test-path "$($this.location)\updates")) {
(mkdir "$($this.location)\updates")
}
$this.GetAssetUrl()
Invoke-WebRequest $this.assetUrl -OutFile "$($this.location)\updates\MBserver.zip" -UseBasicParsing
$this.release.tag_name > "$($this.location)\updates\MBserver.zip.ver"
}
else {
Write-Progress "Emby is up to date $($this.serverInfo.Version)"
Start-Sleep 1
if ($this.serverInfo.HasPendingRestart) {
Write-Progress "Restarting emby"
Start-Sleep 1
Restart-Service $($this.serviceName)
}
}
}
catch {
if (Test-Path "$($this.location)\updates") {
(Remove-Item "$($this.location)\updates" -Recurse)
}
}
}
Install() {
if (Test-Path "$($this.location)\Updates\MBserver.zip") {
Stop-Service $($this.serviceName)
Wait-Process embyserver -ErrorAction SilentlyContinue
Wait-Process MediaBrowser.ServerApplication -ErrorAction SilentlyContinue
if (Test-Path "$($this.location)\System.old") {
Remove-Item "$($this.location)\System.old" -Recurse
}
if (Test-Path "$($this.location)\System") {
Move-Item "$($this.location)\System" "$($this.location)\System.old"
}
if (-not (Test-Path "$($this.location)\System")) {
try {
Install-PackageProvider -Name NuGet -Force
Install-Module -Name 7Zip4Powershell -Force
Import-Module -name 7Zip4Powershell
Expand-7Zip "$($this.location)\Updates\MBserver.zip" "$($this.location)"
}
catch {
if (Test-Path "$($this.location)\System") {
Remove-Item "$($this.location)\System" -Recurse
}
if (Test-Path "$($this.location)\System.old") {
Move-Item "$($this.location)\System.old" "$($this.location)\System"
}
}
}
if (Test-Path "$($this.location)\System") {
Remove-Item "$($this.location)\updates" -Recurse
}
Start-Service $($this.serviceName)
}
}
InstallTask($scriptName) {
Write-Progress "Installing Task"
Start-Sleep 1
if (-not (Test-Path "$($this.location)\updater")) {
mkdir "$($this.location)\updater"
}
Copy-Item "$PSScriptRoot\$scriptName" "$($this.location)\updater\EmbyServiceUpdater.ps1" -Force
if (Get-Command New-ScheduledTaskAction -ErrorAction SilentlyContinue) {
$action = New-ScheduledTaskAction -Execute "Powershell.exe" `
-Argument "-ExecutionPolicy Bypass -file `"$($this.location)\updater\EmbyServiceUpdater.ps1`" -ApiKey $($this.apiKey) -ServerUrl $($this.serverUrl)"
$trigger = New-ScheduledTaskTrigger -Daily -At 4am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Emby Service Updater" -Description "Emby Service Updater" -User "SYSTEM" -Force
}
else {
start-Process "schtasks.exe" -ArgumentList "/create", "/sc DAILY", "/TN `"Emby Service Updater`"",
"/RU SYSTEM", "/TR" , "Powershell.exe", "-ExecutionPolicy Bypass -file `"$($this.location)\updater\EmbyServiceUpdater.ps1`" -ApiKey $($this.apiKey) -ServerUrl $($this.serverUrl)",
"/ST 04:00", "/F" -Wait
}
}
UninstallTask() {
Write-Progress "Uninstalling Task"
Start-Sleep 1
if (Get-Command Unregister-ScheduledTask -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName "Emby Service Updater" -Confirm:$false
}
else {
start-Process "schtasks.exe" -ArgumentList "/Delete", "/TN `"Emby Service Updater`"", "/F" -Wait
}
if (Test-Path "$($this.location)\updater") {
Remove-Item "$($this.location)\updater" -Recurse
}
}
UpdateScript() {
$gitStr = [string](Invoke-RestMethod "https://raw.githubusercontent.com/hatharry/Emby.ServerUpdater/master/EmbyServiceUpdater.ps1" -UseBasicParsing)
$fileStr = [string](Get-Content "$($this.location)\updater\EmbyServiceUpdater.ps1" -Raw)
if ($fileStr.GetHashCode() -ne $gitStr.GetHashCode()) {
Write-Progress "Script Updated"
Start-Sleep 1
$gitStr | Set-Content "$($this.location)\updater\EmbyServiceUpdater.ps1" -NoNewline
}
}
}
$Updater = [EmbyServiceUpdater]::new($ServerUrl, $ApiKey)
if ($installTask) {
$Updater.InstallTask($MyInvocation.MyCommand.Name)
}
elseif ($uninstallTask) {
$Updater.UninstallTask()
}
elseif ($UpdateScript) {
$Updater.UpdateScript()
}
else {
$Updater.GetUpdate()
$Updater.Install()
}