Repository navigation
Expand file tree
/
Copy pathCommon.ps1
More file actions
354 lines (304 loc) · 11.9 KB
/
Copy pathCommon.ps1
File metadata and controls
354 lines (304 loc) · 11.9 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# WinUpdate MSP Helper - shared in-memory helpers
# This file is intended to be loaded from a trusted raw HTTPS URL.
# Process-scope only. This allows trusted in-memory helpers and installed
# PowerShell modules to load without changing LocalMachine/User policy.
try {
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force -ErrorAction Stop
$global:WinUpdateMspExecutionPolicyError = $null
} catch {
$global:WinUpdateMspExecutionPolicyError = $_.Exception.Message
}
if ($global:WinUpdateMspCommonLoaded) { return }
$global:WinUpdateMspCommonLoaded = $true
$global:WinUpdateMspDefaultBaseUrl = 'https://raw.githubusercontent.com/TheTrueZeroTwo/winupdate/main'
$global:WinUpdateMspAllowedScripts = @(
'Common.ps1',
'menu.ps1',
'Invoke-WinUpdate.ps1',
'Install-Winget.ps1',
'DiskCheck.ps1',
'Get-SystemSnapshot.ps1',
'Get-EventSummary.ps1',
'Get-NetworkInfo.ps1',
'Repair-Windows.ps1',
'Install-Task.ps1',
'Test-NetworkConnectivity.ps1'
)
function Set-WumProcessExecutionPolicy {
try {
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force -ErrorAction Stop
$effective = Get-ExecutionPolicy
$list = Get-ExecutionPolicy -List
return [PSCustomObject]@{
Success = $true
Effective = [string]$effective
Process = [string]($list | Where-Object Scope -eq 'Process' | Select-Object -ExpandProperty ExecutionPolicy)
MachinePolicy = [string]($list | Where-Object Scope -eq 'MachinePolicy' | Select-Object -ExpandProperty ExecutionPolicy)
UserPolicy = [string]($list | Where-Object Scope -eq 'UserPolicy' | Select-Object -ExpandProperty ExecutionPolicy)
Error = $null
}
} catch {
return [PSCustomObject]@{
Success = $false
Effective = [string](Get-ExecutionPolicy)
Process = [string](Get-ExecutionPolicy -Scope Process)
MachinePolicy = [string](Get-ExecutionPolicy -Scope MachinePolicy)
UserPolicy = [string](Get-ExecutionPolicy -Scope UserPolicy)
Error = $_.Exception.Message
}
}
}
function Set-WumTls {
try {
[Net.ServicePointManager]::SecurityProtocol =
[Net.SecurityProtocolType]::Tls12 -bor
[Net.SecurityProtocolType]::Tls11 -bor
[Net.SecurityProtocolType]::Tls
} catch {
try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch { }
}
}
function Get-WumBaseUrl {
if ($env:WINUPDATE_BASEURL -and -not [string]::IsNullOrWhiteSpace($env:WINUPDATE_BASEURL)) {
return $env:WINUPDATE_BASEURL.TrimEnd('/')
}
return $global:WinUpdateMspDefaultBaseUrl
}
function Get-WumRootPath {
if ($env:ProgramData -and -not [string]::IsNullOrWhiteSpace($env:ProgramData)) {
return (Join-Path $env:ProgramData 'WinUpdateMspHelper')
}
return (Join-Path $env:TEMP 'WinUpdateMspHelper')
}
function Get-WumLogPath {
return (Join-Path (Get-WumRootPath) 'Logs')
}
function New-WumDirectory {
param([Parameter(Mandatory = $true)][string]$Path)
if (-not (Test-Path -LiteralPath $Path)) {
New-Item -ItemType Directory -Force -Path $Path | Out-Null
}
}
function Start-WumLog {
param([string]$Name = 'session')
$logRoot = Get-WumLogPath
New-WumDirectory -Path $logRoot
$safeName = ($Name -replace '[^a-zA-Z0-9_.-]', '_')
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$path = Join-Path $logRoot ("$stamp-$safeName.log")
$global:WinUpdateMspLastLog = $path
try {
Start-Transcript -Path $path -Append -Force -ErrorAction Stop | Out-Null
} catch {
Write-Warning "Unable to start transcript log: $($_.Exception.Message)"
}
Write-Host "Log: $path" -ForegroundColor DarkGray
return $path
}
function Stop-WumLog {
try { Stop-Transcript | Out-Null } catch { }
}
function Write-WumSection {
param([Parameter(Mandatory = $true)][string]$Title)
Write-Host ''
Write-Host ('=' * 72) -ForegroundColor Cyan
Write-Host $Title -ForegroundColor Cyan
Write-Host ('=' * 72) -ForegroundColor Cyan
}
function Write-WumStep {
param([Parameter(Mandatory = $true)][string]$Message)
Write-Host "[+] $Message" -ForegroundColor Green
}
function Write-WumWarn {
param([Parameter(Mandatory = $true)][string]$Message)
Write-Host "[!] $Message" -ForegroundColor Yellow
}
function Write-WumErrorLine {
param([Parameter(Mandatory = $true)][string]$Message)
Write-Host "[X] $Message" -ForegroundColor Red
}
function Test-WumAdmin {
try {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
} catch {
return $false
}
}
function Assert-WumAdmin {
if (-not (Test-WumAdmin)) {
throw "This helper must be run from an elevated PowerShell session.`nRight-click PowerShell and choose Run as administrator, then run the one-line command again."
}
}
function Get-WumWebText {
param([Parameter(Mandatory = $true)][string]$Uri)
Set-WumTls
$oldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
try {
try {
$response = Invoke-WebRequest -Uri $Uri -UseBasicParsing -ErrorAction Stop
return [string]$response.Content
} catch {
$wc = New-Object System.Net.WebClient
try {
$wc.Headers.Add('User-Agent', 'WinUpdateMspHelper/1.0')
return $wc.DownloadString($Uri)
} finally {
$wc.Dispose()
}
}
} finally {
$ProgressPreference = $oldProgressPreference
}
}
function ConvertTo-WumParameterHashtable {
param([object[]]$ArgumentList = @())
$parameters = @{}
for ($index = 0; $index -lt $ArgumentList.Count; $index++) {
$token = $ArgumentList[$index]
if (-not ($token -is [string]) -or $token -notmatch '^-([A-Za-z][A-Za-z0-9_-]*)$') {
throw "Unsupported positional remote-script argument at index ${index}: $token. Use named parameters."
}
$name = $Matches[1]
$hasValue = $false
$value = $true
if (($index + 1) -lt $ArgumentList.Count) {
$next = $ArgumentList[$index + 1]
if (-not (($next -is [string]) -and $next -match '^-([A-Za-z][A-Za-z0-9_-]*)$')) {
$value = $next
$hasValue = $true
}
}
$parameters[$name] = $value
if ($hasValue) { $index++ }
}
return $parameters
}
function Invoke-WumRemoteScript {
param(
[Parameter(Mandatory = $true)]
[ValidateSet('Invoke-WinUpdate.ps1','Install-Winget.ps1','DiskCheck.ps1','Get-SystemSnapshot.ps1','Get-EventSummary.ps1','Get-NetworkInfo.ps1','Repair-Windows.ps1','Install-Task.ps1','Test-NetworkConnectivity.ps1','menu.ps1')]
[string]$Name,
[hashtable]$Parameters = @{},
# Backward compatibility for older menu/task calls. Named tokens are
# converted to a hashtable before invoking the remote script.
[object[]]$ArgumentList = @()
)
if ($Parameters.Count -gt 0 -and $ArgumentList.Count -gt 0) {
throw 'Use either -Parameters or -ArgumentList, not both.'
}
$baseUrl = Get-WumBaseUrl
$url = "$baseUrl/$Name"
Write-WumSection "Loading remote script: $Name"
Write-Host "Source: $url" -ForegroundColor DarkGray
$code = Get-WumWebText -Uri $url
if ([string]::IsNullOrWhiteSpace($code)) {
throw "Remote script was empty: $url"
}
$policyStatus = Set-WumProcessExecutionPolicy
if (-not $policyStatus.Success) {
Write-WumWarn "Could not set process-only execution policy Bypass: $($policyStatus.Error)"
}
$scriptBlock = [ScriptBlock]::Create($code)
if ($Parameters.Count -gt 0) {
& $scriptBlock @Parameters
return
}
if ($ArgumentList.Count -gt 0) {
$convertedParameters = ConvertTo-WumParameterHashtable -ArgumentList $ArgumentList
& $scriptBlock @convertedParameters
return
}
& $scriptBlock
}
function Invoke-WumNativeCommand {
param(
[Parameter(Mandatory = $true)][string]$FilePath,
[string[]]$Arguments = @(),
[switch]$IgnoreExitCode
)
Write-Host (">> {0} {1}" -f $FilePath, ($Arguments -join ' ')) -ForegroundColor DarkGray
& $FilePath @Arguments
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0 -and -not $IgnoreExitCode) {
throw "Command failed with exit code ${exitCode}: $FilePath $($Arguments -join ' ')"
}
return $exitCode
}
function Test-WumPendingReboot {
$pending = $false
$paths = @(
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending',
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
)
foreach ($path in $paths) {
if (Test-Path $path) { $pending = $true }
}
try {
$sessionManager = Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -ErrorAction Stop
if ($sessionManager.PendingFileRenameOperations) { $pending = $true }
} catch { }
try {
$active = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName' -ErrorAction Stop).ComputerName
$pendingName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName' -ErrorAction Stop).ComputerName
if ($active -and $pendingName -and ($active -ne $pendingName)) { $pending = $true }
} catch { }
return $pending
}
function Get-WumOsSummary {
try {
$os = Get-CimInstance Win32_OperatingSystem -ErrorAction Stop
[PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
Caption = $os.Caption
Version = $os.Version
BuildNumber = $os.BuildNumber
InstallDate = $os.InstallDate
LastBoot = $os.LastBootUpTime
UptimeDays = [math]::Round(((Get-Date) - $os.LastBootUpTime).TotalDays, 2)
}
} catch {
[PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
Caption = 'Unknown'
Version = 'Unknown'
BuildNumber = 'Unknown'
InstallDate = $null
LastBoot = $null
UptimeDays = $null
}
}
}
function Save-WumJson {
param(
[Parameter(Mandatory = $true)]$InputObject,
[Parameter(Mandatory = $true)][string]$Name
)
$root = Join-Path (Get-WumRootPath) 'Reports'
New-WumDirectory -Path $root
$safeName = ($Name -replace '[^a-zA-Z0-9_.-]', '_')
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$path = Join-Path $root ("$stamp-$safeName.json")
$InputObject | ConvertTo-Json -Depth 8 | Set-Content -Path $path -Encoding UTF8
Write-Host "Report: $path" -ForegroundColor DarkGray
return $path
}
function Get-WumMenuOneLiner {
param([string]$BaseUrl = (Get-WumBaseUrl))
$base = $BaseUrl.TrimEnd('/')
$u = "$base/menu.ps1"
$r = "$base/README.md"
return ('Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force; $u=' + "'$u'" + '; $r=' + "'$r'" + '; try { iex (irm $u) } catch { try { [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; iex ((New-Object Net.WebClient).DownloadString($u)) } catch { Write-Host ("Failed to load menu. Open README: " + $r) -ForegroundColor Yellow; throw } }')
}
function Get-WumReadmeUrl {
param([string]$BaseUrl = (Get-WumBaseUrl))
return "$($BaseUrl.TrimEnd('/'))/README.md"
}
function Pause-WumConsole {
param([string]$Message = 'Press Enter to continue')
try { [void](Read-Host $Message) } catch { }
}
Set-WumProcessExecutionPolicy | Out-Null
Set-WumTls