/
Download_SophiApp.ps1
87 lines (76 loc) · 2.69 KB
/
Download_SophiApp.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
<#
.SYNOPSIS
Download the latest SophiApp version
.EXAMPLE
iwr app.sophia.team -useb | iex
#>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if ($Host.Version.Major -eq 5)
{
# Progress bar can significantly impact cmdlet performance
# https://github.com/PowerShell/PowerShell/issues/2138
$Script:ProgressPreference = "SilentlyContinue"
}
if ((Get-Location).Path -eq $env:USERPROFILE)
{
$DownloadsFolder = Get-ItemPropertyValue -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{374DE290-123F-4565-9164-39C4925E467B}"
}
elseif ((Get-Location).Path -eq "$env:SystemRoot\System32")
{
$DownloadsFolder = Get-ItemPropertyValue -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{374DE290-123F-4565-9164-39C4925E467B}"
}
else
{
$DownloadsFolder = (Get-Location).Path
}
$Parameters = @{
Uri = "https://raw.githubusercontent.com/Sophia-Community/SophiApp/master/sophiapp_versions.json"
UseBasicParsing = $true
}
$LatestRelease = (Invoke-RestMethod @Parameters).SophiApp_release
$Parameters = @{
Uri = "https://github.com/Sophia-Community/SophiApp/releases/download/$LatestRelease/SophiApp.zip"
OutFile = "$DownloadsFolder\SophiApp.zip"
UseBasicParsing = $true
Verbose = $true
}
Invoke-WebRequest @Parameters
$Parameters = @{
Path = "$DownloadsFolder\SophiApp.zip"
DestinationPath = "$DownloadsFolder"
Force = $true
}
Expand-Archive @Parameters
Remove-Item -Path "$DownloadsFolder\SophiApp.zip" -Force
Start-Sleep -Second 1
Invoke-Item -Path "$DownloadsFolder\SophiApp"
$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]))
{
try
{
Add-Type @SetForegroundWindow
}
catch [System.ComponentModel.Win32Exception]
{
Write-Warning -Message "PowerShell 5.1 does not compile code if the username contains non-Latin characters (including emoji) and is written in lowercase. Ignore this error message. Open background folder manually."
}
}
Start-Sleep -Seconds 1
Get-Process -Name explorer | Where-Object -FilterScript {$_.MainWindowTitle -eq "SophiApp"} | ForEach-Object -Process {
# Show window, if minimized
[WinAPI.ForegroundWindow]::ShowWindowAsync($_.MainWindowHandle, 5)
# Force move the console window to the foreground
[WinAPI.ForegroundWindow]::SetForegroundWindow($_.MainWindowHandle)
} | Out-Null