Files
winutil/functions/public/Invoke-WPFShortcut.ps1
T

73 lines
2.7 KiB
PowerShell
Raw Normal View History

+2
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
+2
2023-05-09 13:14:27 -05:00
.PARAMETER RunAsAdmin
A boolean value to make 'Run as administrator' property on (true) or off (false), defaults to off
+2
2023-05-09 13:14:27 -05:00
#>
param(
$ShortcutToAdd,
[bool]$RunAsAdmin = $false
)
+2
2023-05-09 13:14:27 -05:00
# Preper the Shortcut Fields and add an a Custom Icon if it's available, else don't add a Custom Icon.
+26
2024-06-04 20:27:27 -07:00
Switch ($ShortcutToAdd) {
"WinUtil" {
# Use Powershell 7 if installed and fallback to PS5 if not
if (Get-Command "pwsh" -ErrorAction SilentlyContinue) {
$shell = "pwsh.exe"
} else {
$shell = "powershell.exe"
}
2024-07-16 21:02:31 +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
+26
2024-06-04 20:27:27 -07:00
$DestinationName = "WinUtil.lnk"
+2
2023-05-09 13:14:27 -05:00
}
+26
2024-06-04 20:27:27 -07:00
}
+2
2023-05-09 13:14:27 -05:00
+26
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
+2
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
+26
2024-06-04 20:27:27 -07:00
# Do an Early Return if the Save Operation was canceled by User's Input.
+26
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
}
+2
2023-05-09 13:14:27 -05:00
# Prepare the Shortcut paramter
+2
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"]
}
if (Test-Path -Path $winutildir["logo.ico"]) {
$shortcut.IconLocation = $winutildir["logo.ico"]
2023-12-19 13:55:55 -06:00
}
# Save the Shortcut to disk
+2
2023-05-09 13:14:27 -05:00
$Shortcut.Save()
2023-10-19 17:12:55 -05: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"
+26
2024-06-04 20:27:27 -07:00
}