Modify Shortcut creation to use PS7 if possible

This commit is contained in:
Martin Wiethan 2024-07-09 20:28:45 +02:00
parent e262af617d
commit a887ad433f

View File

@ -17,14 +17,20 @@ function Invoke-WPFShortcut {
[bool]$RunAsAdmin = $false
)
# Preper the Shortcut Fields and add an a Custom Icon if it's available at "$env:TEMP\cttlogo.png", else don't add a Custom Icon.
# add an a Custom Icon if it's available at "$env:TEMP\cttlogo.png", else don't add a Custom Icon.
$iconPath = $null
Switch ($ShortcutToAdd) {
"WinUtil" {
$SourceExe = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
$IRM = 'irm https://christitus.com/win | iex'
$Powershell = '-ExecutionPolicy Bypass -Command "Start-Process powershell.exe -verb runas -ArgumentList'
$ArgumentsToSourceExe = "$powershell '$IRM'"
# Use Powershell 7 if installed and fallback to PS5 if not
if (Get-Command "pwsh" -ErrorAction SilentlyContinue){
$shell = "pwsh.exe"
}
else{
$shell = "powershell.exe"
}
$shellArgs = "-ExecutionPolicy Bypass -Command `"Start-Process $shell -verb runas -ArgumentList `'-Command `"irm https://christitus.com/win | iex`"`'"
$DestinationName = "WinUtil.lnk"
Invoke-WebRequest -Uri "https://christitus.com/images/logo-full.png" -OutFile "$env:TEMP\cttlogo.png"
@ -52,9 +58,9 @@ function Invoke-WPFShortcut {
# Prepare the Shortcut paramter
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName)
$Shortcut.TargetPath = $SourceExe
$Shortcut.Arguments = $ArgumentsToSourceExe
if ($iconPath -ne $null) {
$Shortcut.TargetPath = $shell
$Shortcut.Arguments = $shellArgs
if ($null -ne $iconPath) {
$shortcut.IconLocation = $iconPath
}