forked from farag2/Sophia-Script-for-Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download_Sophia.ps1
245 lines (217 loc) · 8.01 KB
/
Download_Sophia.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
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
<#
.SYNOPSIS
Download the latest Sophia Script version, depending on what Windows or PowerShell versions are used to
E.g., if you start script on Windows 11 via PowerShell 5.1 you will start downloading Sophia Script for Windows 11 PowerShell 5.1
.EXAMPLE Download and the Sophia Script archive
irm script.sophi.app | iex
.EXAMPLE Download and expand the Wrapper archive
iex "& {$(irm script.sophi.app)} -Wrapper"
.NOTES
Current user
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $false)]
[switch]
$Wrapper
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Parameters = @{
Uri = "https://api.github.com/repos/farag2/Sophia-Script-for-Windows/releases/latest"
UseBasicParsing = $true
}
$LatestGitHubRelease = (Invoke-RestMethod @Parameters).tag_name
$DownloadsFolder = Get-ItemPropertyValue -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{374DE290-123F-4565-9164-39C4925E467B}"
if ($Wrapper)
{
$Parameters = @{
Uri = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).Sophia_Script_Wrapper
$Parameters = @{
Uri = "https://github.com/farag2/Sophia-Script-for-Windows/releases/download/$LatestGitHubRelease/Sophia.Script.Wrapper.v$LatestRelease.zip"
OutFile = "$DownloadsFolder\Sophia.Script.Wrapper.zip"
UseBasicParsing = $true
Verbose = $true
}
Invoke-WebRequest @Parameters
$Version = "Wrapper"
$Parameters = @{
Path = "$DownloadsFolder\Sophia.Script.Wrapper.zip"
DestinationPath = "$DownloadsFolder"
Force = $true
}
Expand-Archive @Parameters
Remove-Item -Path "$DownloadsFolder\Sophia.Script.Wrapper.zip" -Force
Start-Sleep -Second 1
Invoke-Item -Path "$DownloadsFolder\Sophia Script Wrapper v$LatestRelease"
}
switch ((Get-CimInstance -ClassName Win32_OperatingSystem).BuildNumber)
{
"17763"
{
$Parameters = @{
Uri = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_LTSC2019
$Parameters = @{
Uri = "https://github.com/farag2/Sophia-Script-for-Windows/releases/download/$LatestGitHubRelease/Sophia.Script.for.Windows.10.LTSC.2019.v$LatestRelease.zip"
OutFile = "$DownloadsFolder\Sophia.Script.zip"
UseBasicParsing = $true
Verbose = $true
}
$Version = "LTSC2019"
break
}
{($_ -ge 19041) -and ($_ -le 19044)}
{
if ($PSVersionTable.PSVersion.Major -eq 5)
{
# Check if Windows 10 is an LTSC 2021
if ((Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName) -eq "Windows 10 Enterprise LTSC 2021")
{
$Parameters = @{
Uri = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_LTSC2021
$Parameters = @{
Uri = "https://github.com/farag2/Sophia-Script-for-Windows/releases/download/$LatestGitHubRelease/Sophia.Script.for.Windows.10.LTSC.2021.v$LatestRelease.zip"
OutFile = "$DownloadsFolder\Sophia.Script.zip"
UseBasicParsing = $true
Verbose = $true
}
$Version = "LTSC2021"
}
else
{
$Parameters = @{
Uri = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_PowerShell_5_1
$Parameters = @{
Uri = "https://github.com/farag2/Sophia-Script-for-Windows/releases/download/$LatestGitHubRelease/Sophia.Script.for.Windows.10.v$LatestRelease.zip"
OutFile = "$DownloadsFolder\Sophia.Script.zip"
UseBasicParsing = $true
Verbose = $true
}
$Version = "Windows_10_PowerShell_5.1"
}
}
else
{
$Parameters = @{
Uri = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_PowerShell_7
$Parameters = @{
Uri = "https://github.com/farag2/Sophia-Script-for-Windows/releases/download/$LatestGitHubRelease/Sophia.Script.for.Windows.10.v$LatestRelease.PowerShell.7.zip"
OutFile = "$DownloadsFolder\Sophia.Script.zip"
UseBasicParsing = $true
Verbose = $true
}
$Version = "Windows_10_PowerShell_7"
}
}
"22000"
{
if ($PSVersionTable.PSVersion.Major -eq 5)
{
$Parameters = @{
Uri = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_11_PowerShell_5_1
$Parameters = @{
Uri = "https://github.com/farag2/Sophia-Script-for-Windows/releases/download/$LatestGitHubRelease/Sophia.Script.for.Windows.11.v$LatestRelease.zip"
OutFile = "$DownloadsFolder\Sophia.Script.zip"
UseBasicParsing = $true
Verbose = $true
}
$Version = "Windows_11_PowerShell_5.1"
}
else
{
$Parameters = @{
Uri = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_11_PowerShell_7
$Parameters = @{
Uri = "https://github.com/farag2/Sophia-Script-for-Windows/releases/download/$LatestGitHubRelease/Sophia.Script.for.Windows.11.v$LatestRelease.PowerShell.7.zip"
OutFile = "$DownloadsFolder\Sophia.Script.zip"
UseBasicParsing = $true
Verbose = $true
}
$Version = "Windows_11_PowerShell_7"
}
}
}
Invoke-WebRequest @Parameters
$Parameters = @{
Path = "$DownloadsFolder\Sophia.Script.zip"
DestinationPath = "$DownloadsFolder"
Force = $true
}
Expand-Archive @Parameters
Remove-Item -Path "$DownloadsFolder\Sophia.Script.zip" -Force
Start-Sleep -Second 1
switch ($Version)
{
"Wrapper"
{
Invoke-Item -Path "$DownloadsFolder\Sophia Script Wrapper v$LatestRelease"
}
"LTSC2019"
{
Invoke-Item -Path "$DownloadsFolder\Sophia Script for Windows 10 LTSC 2019 v$LatestRelease"
}
"LTSC2021"
{
Invoke-Item -Path "$DownloadsFolder\Sophia Script for Windows 10 LTSC 2021 v$LatestRelease"
}
"Windows_10_PowerShell_5.1"
{
Invoke-Item -Path "$DownloadsFolder\Sophia Script for Windows 10 v$LatestRelease"
}
"Windows_10_PowerShell_7"
{
Invoke-Item -Path "$DownloadsFolder\Sophia Script for Windows 10 v$LatestRelease PowerShell 7"
}
"Windows_11_PowerShell_5.1"
{
Invoke-Item -Path "$DownloadsFolder\Sophia Script for Windows 11 v$LatestRelease"
}
"Windows_11_PowerShell_7"
{
Invoke-Item -Path "$DownloadsFolder\Sophia Script for Windows 11 v$LatestRelease PowerShell 7"
}
}
$SetForegroundWindow = @{
Namespace = "WinAPI"
Name = "ForegroundWindow"
Language = "CSharp"
MemberDefinition = @"
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
"@
}
if (-not ("WinAPI.ForegroundWindow" -as [type]))
{
Add-Type @SetForegroundWindow
}
Get-Process -Name explorer | Where-Object -FilterScript {$_.MainWindowTitle -match "Sophia Script for Windows $([System.Environment]::OSVersion.Version.Major)"} | ForEach-Object -Process {
# Show window, if minimized
[WinAPI.ForegroundWindow]::ShowWindowAsync($_.MainWindowHandle, 5)
Start-Sleep -Seconds 3
# Force move the console window to the foreground
[WinAPI.ForegroundWindow]::SetForegroundWindow($_.MainWindowHandle)
}