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
This commit is contained in:
Mr.k 2024-05-01 21:42:38 +03:00 committed by GitHub
parent bdf9c8378c
commit f5f506b9c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,27 +17,35 @@ function Invoke-WPFShortcut {
[bool]$RunAsAdmin = $false [bool]$RunAsAdmin = $false
) )
$iconPath = $null # 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.
Switch ($ShortcutToAdd) { $iconPath = $null
"WinUtil" { Switch ($ShortcutToAdd) {
$SourceExe = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" "WinUtil" {
$IRM = 'irm https://christitus.com/win | iex' $SourceExe = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
$Powershell = '-ExecutionPolicy Bypass -Command "Start-Process powershell.exe -verb runas -ArgumentList' $IRM = 'irm https://christitus.com/win | iex'
$ArgumentsToSourceExe = "$powershell '$IRM'" $Powershell = '-ExecutionPolicy Bypass -Command "Start-Process powershell.exe -verb runas -ArgumentList'
$DestinationName = "WinUtil.lnk" $ArgumentsToSourceExe = "$powershell '$IRM'"
$DestinationName = "WinUtil.lnk"
if (Test-Path -Path "$env:TEMP\cttlogo.png") { if (Test-Path -Path "$env:TEMP\cttlogo.png") {
$iconPath = "$env:SystempRoot\cttlogo.ico" $iconPath = "$env:SystempRoot\cttlogo.ico"
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath $iconPath 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 = New-Object System.Windows.Forms.SaveFileDialog
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop') $FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk" $FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk"
$FileBrowser.FileName = $DestinationName $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 $WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName) $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" Write-Host "Shortcut for $ShortcutToAdd has been saved to $($FileBrowser.FileName) with 'Run as administrator' set to $RunAsAdmin"
} }