Fix Icon Support for Shortcut Creation (#2242)

* Make 'ConvertTo-Icon' Private Function Parameter more stricter

* Fix 'Invoke-WPFShortcut' Public Function Icon Support for the created shortcuts

* Replace Tabs with Spaces to follow the conventions

* Add new optional parameter to 'ConvertTo-Icon' and Improve the documentation for the function

* Update the Description for 'ConvertTo-Icon' Function

* Update some Documentation Wording in 'ConvertTo-Icon' Function

* Change behavior of one case from throwing an exception to doing an early return

Co-authored-by: Martin Wiethan <47688561+Marterich@users.noreply.github.com>

* Update Examples for 'ConvertTo-Icon' Functions & Add a new Example, as well as some other changes

Besides the updated documentation for 'ConvertTo-Icon' Function, the icon file path has changed from '$env:TEMP\cttlogo.ico' into '$env:LOCALAPPDATA\winutil\cttlogo.ico', and add edge-case of Folder not being found for the Icon File in 'ConvertTo-Icon' Code.

---------

Co-authored-by: Martin Wiethan <47688561+Marterich@users.noreply.github.com>
This commit is contained in:
Mr.k
2024-07-08 23:14:10 +03:00
committed by GitHub
parent 79afb8c1bd
commit ce7d14b227
2 changed files with 82 additions and 11 deletions

View File

@ -27,8 +27,10 @@ function Invoke-WPFShortcut {
$ArgumentsToSourceExe = "$powershell '$IRM'"
$DestinationName = "WinUtil.lnk"
Invoke-WebRequest -Uri "https://christitus.com/images/logo-full.png" -OutFile "$env:TEMP\cttlogo.png"
if (Test-Path -Path "$env:TEMP\cttlogo.png") {
$iconPath = "$env:SystempRoot\cttlogo.ico"
$iconPath = "$env:LOCALAPPDATA\winutil\cttlogo.ico"
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath $iconPath
}
}
@ -40,20 +42,23 @@ function Invoke-WPFShortcut {
$FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk"
$FileBrowser.FileName = $DestinationName
# Do an Early Return if The Save Shortcut operation was cancel by User's Input.
# Do an Early Return if the Save Operation was canceled by User's Input.
$FileBrowserResult = $FileBrowser.ShowDialog()
$DialogResultEnum = New-Object System.Windows.Forms.DialogResult
if (-not ($FileBrowserResult -eq $DialogResultEnum::OK)) {
return
}
# Prepare the Shortcut paramter
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName)
$Shortcut.TargetPath = $SourceExe
$Shortcut.Arguments = $ArgumentsToSourceExe
if ($null -ne $iconPath) {
if ($iconPath -ne $null) {
$shortcut.IconLocation = $iconPath
}
# Save the Shortcut to disk
$Shortcut.Save()
if ($RunAsAdmin -eq $true) {