From 9291020d12754e9c66c92a3d44583e9284ce8cae Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Thu, 22 Feb 2024 04:12:44 +0300 Subject: [PATCH] Add 'Run as administrator' to 'Invoke-WPFShortcut' Function and Use it in 'Invoke-WPFButton' (#1625) The full details on implementation is found in the commit changes, as well as documentation in 'Invoke-WPFShortcut.ps1' file. --- functions/public/Invoke-WPFButton.ps1 | 2 +- functions/public/Invoke-WPFShortcut.ps1 | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/functions/public/Invoke-WPFButton.ps1 b/functions/public/Invoke-WPFButton.ps1 index 3bd0428f..7fa8638a 100644 --- a/functions/public/Invoke-WPFButton.ps1 +++ b/functions/public/Invoke-WPFButton.ps1 @@ -47,7 +47,7 @@ function Invoke-WPFButton { "WPFFixesNetwork" {Invoke-WPFFixesNetwork} "WPFUpdatesdisable" {Invoke-WPFUpdatesdisable} "WPFUpdatessecurity" {Invoke-WPFUpdatessecurity} - "WPFWinUtilShortcut" {Invoke-WPFShortcut -ShortcutToAdd "WinUtil"} + "WPFWinUtilShortcut" {Invoke-WPFShortcut -ShortcutToAdd "WinUtil" -RunAsAdmin $true} "WPFGetInstalled" {Invoke-WPFGetInstalled -CheckBox "winget"} "WPFGetInstalledTweaks" {Invoke-WPFGetInstalled -CheckBox "tweaks"} "WPFGetIso" {Invoke-WPFGetIso} diff --git a/functions/public/Invoke-WPFShortcut.ps1 b/functions/public/Invoke-WPFShortcut.ps1 index f195ca9a..4eab2dc6 100644 --- a/functions/public/Invoke-WPFShortcut.ps1 +++ b/functions/public/Invoke-WPFShortcut.ps1 @@ -8,8 +8,14 @@ function Invoke-WPFShortcut { .PARAMETER ShortcutToAdd The name of the shortcut to add + .PARAMETER RunAsAdmin + A boolean value to make 'Run as administrator' property on (true) or off (false), defaults to off + #> - param($ShortcutToAdd) + param( + $ShortcutToAdd, + [bool]$RunAsAdmin = $false + ) $iconPath = $null Switch ($ShortcutToAdd) { @@ -42,5 +48,12 @@ function Invoke-WPFShortcut { } $Shortcut.Save() - Write-Host "Shortcut for $ShortcutToAdd has been saved to $($FileBrowser.FileName)" + 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" } \ No newline at end of file