-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstall-Task.ps1
More file actions
166 lines (146 loc) · 6.66 KB
/
Copy pathInstall-Task.ps1
File metadata and controls
166 lines (146 loc) · 6.66 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
param(
[string]$TaskName = 'WinUpdate MSP Helper - Remote Updates',
[ValidateSet('Daily','Weekly','Startup','Once')]
[string]$Frequency = 'Weekly',
[string]$At = '03:00',
[ValidateSet('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')]
[string]$DayOfWeek = 'Sunday',
[ValidateSet('UpdateNoReboot','UpdateIfNeeded','UpdateWithWingetNoReboot','WingetOnly','SystemSnapshot','DiskCheck')]
[string]$TaskAction = 'UpdateIfNeeded',
[switch]$RunAsCurrentUser,
[switch]$Force,
[string]$BaseUrl
)
$ErrorActionPreference = 'Stop'
if ($BaseUrl -and -not [string]::IsNullOrWhiteSpace($BaseUrl)) { $env:WINUPDATE_BASEURL = $BaseUrl.TrimEnd('/') }
$baseUrl = if ($env:WINUPDATE_BASEURL) { $env:WINUPDATE_BASEURL.TrimEnd('/') } else { 'https://raw.githubusercontent.com/TheTrueZeroTwo/winupdate/main' }
try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls } catch { }
$wc = New-Object Net.WebClient
try {
$wc.Headers.Add('User-Agent', 'WinUpdateMspHelper/1.0')
$commonCode = $wc.DownloadString("$baseUrl/Common.ps1")
} finally {
$wc.Dispose()
}
. ([ScriptBlock]::Create($commonCode))
function Get-WumScheduledPayload {
param(
[Parameter(Mandatory=$true)][string]$BaseUrl,
[Parameter(Mandatory=$true)][string]$TaskAction
)
$scriptName = $null
$parametersExpr = '@{}'
switch ($TaskAction) {
'UpdateNoReboot' {
$scriptName = 'Invoke-WinUpdate.ps1'
$parametersExpr = "@{ RebootMode = 'Never' }"
}
'UpdateIfNeeded' {
$scriptName = 'Invoke-WinUpdate.ps1'
$parametersExpr = "@{ RebootMode = 'IfNeeded' }"
}
'UpdateWithWingetNoReboot' {
$scriptName = 'Invoke-WinUpdate.ps1'
$parametersExpr = "@{ RebootMode = 'Never'; IncludeWinget = `$true; InstallWingetIfMissing = `$true }"
}
'WingetOnly' {
$scriptName = 'Invoke-WinUpdate.ps1'
$parametersExpr = "@{ SkipWindowsUpdate = `$true; IncludeWinget = `$true; InstallWingetIfMissing = `$true }"
}
'SystemSnapshot' {
$scriptName = 'Get-SystemSnapshot.ps1'
$parametersExpr = '@{}'
}
'DiskCheck' {
$scriptName = 'DiskCheck.ps1'
$parametersExpr = '@{}'
}
default { throw "Unsupported task action: $TaskAction" }
}
$escapedBase = $BaseUrl.Replace("'", "''")
$payload = @"
`$ErrorActionPreference='Stop'
try { [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls } catch { }
`$env:WINUPDATE_BASEURL='$escapedBase'
`$wc=New-Object Net.WebClient
try {
`$wc.Headers.Add('User-Agent','WinUpdateMspHelperScheduledTask/1.0')
`$common=`$wc.DownloadString('$escapedBase/Common.ps1')
} finally {
try { `$wc.Dispose() } catch { }
}
. ([ScriptBlock]::Create(`$common))
Invoke-WumRemoteScript -Name '$scriptName' -Parameters $parametersExpr
"@
return $payload
}
function New-WumTaskTrigger {
param(
[string]$Frequency,
[string]$At,
[string]$DayOfWeek
)
switch ($Frequency) {
'Daily' {
$time = [datetime]::Parse($At)
return New-ScheduledTaskTrigger -Daily -At $time
}
'Weekly' {
$time = [datetime]::Parse($At)
return New-ScheduledTaskTrigger -Weekly -DaysOfWeek $DayOfWeek -At $time
}
'Startup' {
return New-ScheduledTaskTrigger -AtStartup
}
'Once' {
$when = [datetime]::Parse($At)
if ($when -lt (Get-Date)) { $when = $when.AddDays(1) }
return New-ScheduledTaskTrigger -Once -At $when
}
default { throw "Unsupported frequency: $Frequency" }
}
}
$log = Start-WumLog -Name 'install-scheduled-task'
try {
Assert-WumAdmin
Write-WumSection 'Install scheduled task'
Write-Host 'The scheduled task stores only the task definition and a PowerShell encoded command.' -ForegroundColor DarkGray
Write-Host 'It does not save the repo scripts locally. At run time it loads Common.ps1 and the selected action from the configured web URL.' -ForegroundColor DarkGray
Write-Host "Task name: $TaskName"
Write-Host "Action: $TaskAction"
Write-Host "Schedule: $Frequency $At $DayOfWeek"
Write-Host "Base URL: $baseUrl"
$payload = Get-WumScheduledPayload -BaseUrl $baseUrl -TaskAction $TaskAction
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($payload))
$taskActionObj = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NoProfile -ExecutionPolicy Bypass -EncodedCommand $encoded"
$trigger = New-WumTaskTrigger -Frequency $Frequency -At $At -DayOfWeek $DayOfWeek
$settings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -MultipleInstances IgnoreNew
$existing = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
if ($existing) {
if (-not $Force) {
$replace = Read-Host "Scheduled task already exists. Replace it? (y/N)"
if ($replace -notmatch '^(y|yes)$') {
Write-WumWarn 'Leaving existing scheduled task unchanged.'
return
}
}
Write-WumStep 'Removing existing scheduled task'
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction Stop
}
if ($RunAsCurrentUser) {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent().Name
Write-WumWarn 'Registering as the current interactive user. This is better for winget actions, but it may not run when nobody is logged on.'
$principal = New-ScheduledTaskPrincipal -UserId $identity -LogonType Interactive -RunLevel Highest
} else {
Write-WumStep 'Registering as SYSTEM with highest privileges'
$principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest
}
Register-ScheduledTask -TaskName $TaskName -Action $taskActionObj -Trigger $trigger -Settings $settings -Principal $principal -Description "WinUpdate MSP Helper remote action: $TaskAction from $baseUrl" -Force | Out-Null
Write-WumSection 'Scheduled task installed'
Get-ScheduledTask -TaskName $TaskName | Format-List TaskName, TaskPath, State, Author, Description
Write-Host ''
Write-Host 'Run now from an elevated PowerShell prompt:' -ForegroundColor Yellow
Write-Host "Start-ScheduledTask -TaskName '$TaskName'" -ForegroundColor White
} finally {
Stop-WumLog
}