From f5f506b9c50d4c9ef93666f355d08beef645854f Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Wed, 1 May 2024 21:42:38 +0300 Subject: [PATCH] Simple improvement: make WinUtil's Shortcut Function Do Less (Lazier) (#1903) * Add Extra Comments to 'Invoke-WPFShortcut' to make the Logic a Bit Clearer * Make The 'Invoke-WPFShortcut' Function Lazier for Added Efficiency and to not Display False Info whenever the User Cancels the 'Save As' Operation --- functions/public/Invoke-WPFShortcut.ps1 | 36 +++++++++++++++---------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/functions/public/Invoke-WPFShortcut.ps1 b/functions/public/Invoke-WPFShortcut.ps1 index 4eab2dc6..f8ae04f9 100644 --- a/functions/public/Invoke-WPFShortcut.ps1 +++ b/functions/public/Invoke-WPFShortcut.ps1 @@ -17,27 +17,35 @@ function Invoke-WPFShortcut { [bool]$RunAsAdmin = $false ) - $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'" - $DestinationName = "WinUtil.lnk" + # 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. + $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'" + $DestinationName = "WinUtil.lnk" - if (Test-Path -Path "$env:TEMP\cttlogo.png") { - $iconPath = "$env:SystempRoot\cttlogo.ico" - ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath $iconPath - } + if (Test-Path -Path "$env:TEMP\cttlogo.png") { + $iconPath = "$env:SystempRoot\cttlogo.ico" + ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath $iconPath } } + } + # Show a File Dialog Browser, to let the User choose the Name and Location of where to save the Shortcut $FileBrowser = New-Object System.Windows.Forms.SaveFileDialog $FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop') $FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk" $FileBrowser.FileName = $DestinationName - $FileBrowser.ShowDialog() | Out-Null + + # Do an Early Return if The Save Shortcut operation was cancel by User's Input. + $FileBrowserResult = $FileBrowser.ShowDialog() + $DialogResultEnum = New-Object System.Windows.Forms.DialogResult + if (-not ($FileBrowserResult -eq $DialogResultEnum::OK)) { + return + } $WshShell = New-Object -comObject WScript.Shell $Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName) @@ -56,4 +64,4 @@ function Invoke-WPFShortcut { } Write-Host "Shortcut for $ShortcutToAdd has been saved to $($FileBrowser.FileName) with 'Run as administrator' set to $RunAsAdmin" -} \ No newline at end of file +}