-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-CWPatching.ps1
More file actions
175 lines (150 loc) · 5.54 KB
/
Copy pathGet-CWPatching.ps1
File metadata and controls
175 lines (150 loc) · 5.54 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
<#
.SYNOPSIS
Cloudwave EUC toolset import.
For questions, contact Dan Stark / Cloudwave EUC
#>
if ($PSVersionTable.PSEdition -ne "Desktop") {
Write-Host "Run this from Windows PowerShell 5.1, not PowerShell 7." -ForegroundColor Red
exit 1
}
$api = "https://api.github.com/repos/DanStarkTX/cw_patching/contents"
$EventSource = "EUC Script Import"
$LogName = "Application"
function Write-ImportEventLog {
param(
[string]$Message,
[ValidateSet('Information','Warning','Error')]
[string]$EntryType = 'Information',
[int]$EventId = 1030
)
try {
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
[System.Diagnostics.EventLog]::CreateEventSource($EventSource, $LogName)
}
$log = New-Object System.Diagnostics.EventLog($LogName)
$log.Source = $EventSource
$log.WriteEntry($Message, [System.Diagnostics.EventLogEntryType]::$EntryType, $EventId)
} catch {
try {
$log = New-Object System.Diagnostics.EventLog($LogName)
$log.Source = "WindowsUpdateScript"
$log.WriteEntry("[EUC Script Import] $Message", [System.Diagnostics.EventLogEntryType]::$EntryType, $EventId)
} catch { }
}
}
Write-Host ""
Write-Host "=== Cloudwave EUC Toolset Import ===" -ForegroundColor Cyan
Write-Host ""
Write-ImportEventLog -Message "Cloudwave EUC Toolset Import started on $env:COMPUTERNAME." -EventId 1030
$null = cmd /c "mkdir C:\cwave 2>nul"
$null = cmd /c "mkdir C:\cwave\scripts 2>nul"
$null = cmd /c "mkdir C:\cwave\scripts\functions 2>nul"
$null = cmd /c "mkdir C:\cwave\scripts\config 2>nul"
$null = cmd /c "mkdir C:\cwave\scripts\modules 2>nul"
$null = cmd /c "mkdir C:\cwave\scripts\modules\PSWindowsUpdate 2>nul"
$null = cmd /c "mkdir C:\cwave\scripts\modules\PSWindowsUpdate\2.2.1.5 2>nul"
function cwGet {
param([string]$p)
try {
$result = Invoke-RestMethod -Uri "$api/$p" -ErrorAction Stop
$items = @()
foreach ($item in $result) {
if ($item.type -eq 'file' -and $item.download_url -and $item.name) {
$items += $item
}
}
$items
} catch {
@()
}
}
function cwSave {
param([string]$url, [string]$out)
try {
Invoke-WebRequest -Uri $url -OutFile $out -UseBasicParsing -ErrorAction Stop
$true
} catch {
$false
}
}
function Get-GitBlobSHA {
param([string]$Path)
try {
$content = [System.IO.File]::ReadAllBytes($Path)
$header = [System.Text.Encoding]::ASCII.GetBytes("blob $($content.Length)`0")
$combined = $header + $content
$sha1 = [System.Security.Cryptography.SHA1]::Create()
$hashBytes = $sha1.ComputeHash($combined)
($hashBytes | ForEach-Object { $_.ToString("x2") }) -join ""
} catch {
$null
}
}
$fileList = @()
# Root files
foreach ($f in @(cwGet "" | Where-Object { $_.name -match '\.bat$|\.ps1$' -and $_.name -ne 'Get-CWPatching.ps1' })) {
$fileList += [PSCustomObject]@{ Url = $f.download_url; Out = "C:\cwave\$($f.name)"; SHA = $f.sha }
}
# Scripts
foreach ($f in cwGet "scripts") {
$fileList += [PSCustomObject]@{ Url = $f.download_url; Out = "C:\cwave\scripts\$($f.name)"; SHA = $f.sha }
}
# Functions
foreach ($f in cwGet "scripts/functions") {
$fileList += [PSCustomObject]@{ Url = $f.download_url; Out = "C:\cwave\scripts\functions\$($f.name)"; SHA = $f.sha }
}
# Config
foreach ($f in cwGet "scripts/config") {
$fileList += [PSCustomObject]@{ Url = $f.download_url; Out = "C:\cwave\scripts\config\$($f.name)"; SHA = $f.sha }
}
# PSWindowsUpdate module
foreach ($f in cwGet "scripts/modules/PSWindowsUpdate/2.2.1.5") {
$fileList += [PSCustomObject]@{ Url = $f.download_url; Out = "C:\cwave\scripts\modules\PSWindowsUpdate\2.2.1.5\$($f.name)"; SHA = $f.sha }
}
$total = $fileList.Count
$imported = 0
$skipped = 0
$importedFiles = @()
for ($i = 0; $i -lt $total; $i++) {
$file = $fileList[$i]
$fileName = Split-Path $file.Out -Leaf
Write-Progress -Activity "Cloudwave EUC Toolset Import" -Status "Checking $fileName" -PercentComplete (($i / $total) * 100)
$needsUpdate = $true
if ($file.SHA -and (Test-Path -LiteralPath $file.Out)) {
$localSHA = Get-GitBlobSHA -Path $file.Out
if ($localSHA -and $localSHA -eq $file.SHA) {
$needsUpdate = $false
}
}
if ($needsUpdate) {
Write-Progress -Activity "Cloudwave EUC Toolset Import" -Status "Importing $fileName" -PercentComplete (($i / $total) * 100)
if (cwSave $file.Url $file.Out) {
$imported++
$importedFiles += $fileName
}
} else {
$skipped++
}
}
Write-Progress -Activity "Cloudwave EUC Toolset Import" -Completed
Write-Host ""
if ($skipped -gt 0) {
$summary = "Import complete: $imported file(s) downloaded, $skipped already up to date."
} else {
$summary = "Import complete: $imported files downloaded."
}
Write-Host $summary -ForegroundColor Cyan
if ($importedFiles.Count -gt 0) {
$sb = New-Object System.Text.StringBuilder
[void]$sb.AppendLine($summary)
[void]$sb.AppendLine("")
[void]$sb.AppendLine("Files downloaded:")
foreach ($f in $importedFiles) { [void]$sb.AppendLine(" $f") }
Write-ImportEventLog -Message $sb.ToString() -EventId 1031
}
Write-ImportEventLog -Message $summary -EventId 1032
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Yellow
Write-Host " Run updates: C:\cwave\run_updates.bat" -ForegroundColor White
Write-Host " Run cleanup: C:\cwave\run_cleanup.bat" -ForegroundColor White
Write-Host ""