2023-05-09 13:14:27 -05:00
|
|
|
function Invoke-WPFShortcut {
|
|
|
|
|
<#
|
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
.SYNOPSIS
|
|
|
|
|
Creates a shortcut and prompts for a save location
|
|
|
|
|
|
|
|
|
|
.PARAMETER ShortcutToAdd
|
|
|
|
|
The name of the shortcut to add
|
2023-05-09 13:14:27 -05:00
|
|
|
|
2024-02-22 04:12:44 +03:00
|
|
|
.PARAMETER RunAsAdmin
|
|
|
|
|
A boolean value to make 'Run as administrator' property on (true) or off (false), defaults to off
|
|
|
|
|
|
2023-05-09 13:14:27 -05:00
|
|
|
#>
|
2024-02-22 04:12:44 +03:00
|
|
|
param(
|
|
|
|
|
$ShortcutToAdd,
|
|
|
|
|
[bool]$RunAsAdmin = $false
|
|
|
|
|
)
|
2023-05-09 13:14:27 -05:00
|
|
|
|
2024-07-25 23:19:45 +02:00
|
|
|
# Preper the Shortcut Fields and add an a Custom Icon if it's available, else don't add a Custom Icon.
|
|
|
|
|
|
2024-06-04 20:27:27 -07:00
|
|
|
Switch ($ShortcutToAdd) {
|
|
|
|
|
"WinUtil" {
|
2024-07-15 03:03:12 +02:00
|
|
|
# Use Powershell 7 if installed and fallback to PS5 if not
|
2024-08-06 23:35:17 +03:00
|
|
|
if (Get-Command "pwsh" -ErrorAction SilentlyContinue) {
|
2024-07-15 03:03:12 +02:00
|
|
|
$shell = "pwsh.exe"
|
2024-08-06 23:35:17 +03:00
|
|
|
} else {
|
2024-07-15 03:03:12 +02:00
|
|
|
$shell = "powershell.exe"
|
|
|
|
|
}
|
2024-07-16 21:02:31 +02:00
|
|
|
|
2024-07-25 22:20:51 +02:00
|
|
|
$shellArgs = "-ExecutionPolicy Bypass -Command `"Start-Process $shell -verb runas -ArgumentList `'-Command `"irm https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1 | iex`"`'"
|
2024-07-16 21:02:31 +02:00
|
|
|
|
2024-06-04 20:27:27 -07:00
|
|
|
$DestinationName = "WinUtil.lnk"
|
2023-05-09 13:14:27 -05:00
|
|
|
}
|
2024-06-04 20:27:27 -07:00
|
|
|
}
|
2023-05-09 13:14:27 -05:00
|
|
|
|
2024-06-04 20:27:27 -07:00
|
|
|
# Show a File Dialog Browser, to let the User choose the Name and Location of where to save the Shortcut
|
2023-05-09 13:14:27 -05:00
|
|
|
$FileBrowser = New-Object System.Windows.Forms.SaveFileDialog
|
|
|
|
|
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
|
|
|
|
|
$FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk"
|
|
|
|
|
$FileBrowser.FileName = $DestinationName
|
2024-06-04 20:27:27 -07:00
|
|
|
|
2024-07-08 23:14:10 +03:00
|
|
|
# Do an Early Return if the Save Operation was canceled by User's Input.
|
2024-06-04 20:27:27 -07:00
|
|
|
$FileBrowserResult = $FileBrowser.ShowDialog()
|
|
|
|
|
$DialogResultEnum = New-Object System.Windows.Forms.DialogResult
|
|
|
|
|
if (-not ($FileBrowserResult -eq $DialogResultEnum::OK)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-09 13:14:27 -05:00
|
|
|
|
2024-07-08 23:14:10 +03:00
|
|
|
# Prepare the Shortcut paramter
|
2023-05-09 13:14:27 -05:00
|
|
|
$WshShell = New-Object -comObject WScript.Shell
|
|
|
|
|
$Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName)
|
2024-08-06 19:53:41 +02:00
|
|
|
$Shortcut.TargetPath = $shell
|
|
|
|
|
$Shortcut.Arguments = $shellArgs
|
2024-09-10 03:19:34 +02:00
|
|
|
if (-NOT (Test-Path -Path $winutildir["logo.ico"])) {
|
|
|
|
|
Invoke-WebRequest -Uri "https://christitus.com/images/logo-full.ico" -OutFile $winutildir["logo.ico"]
|
|
|
|
|
}
|
2024-07-25 23:19:45 +02:00
|
|
|
if (Test-Path -Path $winutildir["logo.ico"]) {
|
|
|
|
|
$shortcut.IconLocation = $winutildir["logo.ico"]
|
2023-12-19 13:55:55 -06:00
|
|
|
}
|
2024-07-08 23:14:10 +03:00
|
|
|
|
|
|
|
|
# Save the Shortcut to disk
|
2023-05-09 13:14:27 -05:00
|
|
|
$Shortcut.Save()
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2024-02-22 04:12:44 +03:00
|
|
|
if ($RunAsAdmin -eq $true) {
|
|
|
|
|
$bytes = [System.IO.File]::ReadAllBytes($FileBrowser.FileName)
|
|
|
|
|
# Set byte value at position 0x15 in hex, or 21 in decimal, from the value 0x00 to 0x20 in hex
|
|
|
|
|
$bytes[0x15] = $bytes[0x15] -bor 0x20
|
|
|
|
|
[System.IO.File]::WriteAllBytes($FileBrowser.FileName, $bytes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Write-Host "Shortcut for $ShortcutToAdd has been saved to $($FileBrowser.FileName) with 'Run as administrator' set to $RunAsAdmin"
|
2024-06-04 20:27:27 -07:00
|
|
|
}
|