Skip to content

Commit 4ca4c8a

Browse files
authored
Update Download_SophiApp.ps1
1 parent 79a18e1 commit 4ca4c8a

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

Download_SophiApp.ps1

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
55
.EXAMPLE
66
iwr app.sophia.team -useb | iex
7+
8+
.NOTES
9+
Current user
710
#>
811

912
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
@@ -29,12 +32,12 @@ else
2932
}
3033

3134
$Parameters = @{
32-
Uri = "https://raw.githubusercontent.com/Sophia-Community/SophiApp/master/sophiapp_versions.json"
33-
UseBasicParsing = $true
35+
Uri = "https://api.github.com/repos/Sophia-Community/SophiApp/releases/latest"
36+
UseBasicParsing = $true
3437
}
35-
$LatestRelease = (Invoke-RestMethod @Parameters).SophiApp_release
38+
$LatestRelease = (Invoke-RestMethod @Parameters).assets.browser_download_url
3639
$Parameters = @{
37-
Uri = "https://github.com/Sophia-Community/SophiApp/releases/download/$LatestRelease/SophiApp.zip"
40+
Uri = $LatestRelease
3841
OutFile = "$DownloadsFolder\SophiApp.zip"
3942
UseBasicParsing = $true
4043
Verbose = $true
@@ -52,28 +55,35 @@ Remove-Item -Path "$DownloadsFolder\SophiApp.zip" -Force
5255
Start-Sleep -Second 1
5356
Invoke-Item -Path "$DownloadsFolder\SophiApp"
5457

55-
$SetForegroundWindow = @{
56-
Namespace = "WinAPI"
57-
Name = "ForegroundWindow"
58-
Language = "CSharp"
59-
MemberDefinition = @"
58+
# https://github.com/PowerShell/PowerShell/issues/21070
59+
$CompilerParameters = [System.CodeDom.Compiler.CompilerParameters]::new("System.dll")
60+
$CompilerParameters.TempFiles = [System.CodeDom.Compiler.TempFileCollection]::new($env:TEMP, $false)
61+
$CompilerParameters.GenerateInMemory = $true
62+
$Signature = @{
63+
Namespace = "WinAPI"
64+
Name = "ForegroundWindow"
65+
Language = "CSharp"
66+
CompilerParameters = $CompilerParameters
67+
MemberDefinition = @"
6068
[DllImport("user32.dll")]
6169
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
6270
[DllImport("user32.dll")]
6371
[return: MarshalAs(UnmanagedType.Bool)]
6472
public static extern bool SetForegroundWindow(IntPtr hWnd);
6573
"@
74+
}
75+
76+
# PowerShell 7 has CompilerOptions argument instead of CompilerParameters as PowerShell 5 has
77+
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type#-compileroptions
78+
if ($Host.Version.Major -eq 7)
79+
{
80+
$Signature.Remove("CompilerParameters")
81+
$Signature.Add("CompilerOptions", $CompilerParameters)
6682
}
83+
6784
if (-not ("WinAPI.ForegroundWindow" -as [type]))
6885
{
69-
try
70-
{
71-
Add-Type @SetForegroundWindow
72-
}
73-
catch [System.ComponentModel.Win32Exception]
74-
{
75-
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."
76-
}
86+
Add-Type @Signature
7787
}
7888

7989
Start-Sleep -Seconds 1

0 commit comments

Comments
 (0)