Compile Winutil

This commit is contained in:
ChrisTitusTech 2024-07-08 20:14:32 +00:00 committed by github-actions[bot]
parent ce7d14b227
commit 47dbbfb4ff

View File

@ -66,28 +66,94 @@ function ConvertTo-Icon {
<# <#
.DESCRIPTION .DESCRIPTION
This function will convert PNG to ICO file This function will convert BMP, GIF, EXIF, JPG, PNG and TIFF to ICO file
.PARAMETER bitmapPath
The file path to bitmap image to make '.ico' file out of.
Supported file types according to Microsoft Documentation is the following:
BMP, GIF, EXIF, JPG, PNG and TIFF.
.PARAMETER iconPath
The file path to write the new '.ico' resource.
.PARAMETER overrideIconFile
An optional boolean Parameter that makes the function overrides
the Icon File Path if the file exists. Defaults to $true.
.EXAMPLE .EXAMPLE
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath $iconPath try {
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath "$env:TEMP\cttlogo.ico"
} catch [System.IO.FileNotFoundException] {
# Handle the thrown exception here...
}
This Example makes a '.ico' file at "$env:TEMP\cttlogo.ico" File Path using the bitmap file
found in "$env:TEMP\cttlogo.png", the function overrides the '.ico' File if it's found.
this function will throw a FileNotFound Exception at the event of not finding the provided bitmap File Path.
.EXAMPLE
try {
ConvertTo-Icon "$env:TEMP\cttlogo.png" "$env:TEMP\cttlogo.ico"
} catch [System.IO.FileNotFoundException] {
# Handle the thrown exception here...
}
This Example is the same as Example 1, but uses Positional Parameters instead.
.EXAMPLE
if (Test-Path "$env:TEMP\cttlogo.png") {
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath "$env:TEMP\cttlogo.ico"
}
This Example is same as Example 1, but checks if the bitmap File exists before calling 'ConvertTo-Icon' Function.
This's the recommended way of using this function, as it doesn't require any try-catch blocks.
.EXAMPLE
try {
ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath "$env:TEMP\cttlogo.ico" -overrideIconFile $false
} catch [System.IO.FileNotFoundException] {
# Handle the thrown exception here...
}
This Example make use of '-overrideIconFile' Optional Parameter, the default for this paramter is $true.
By doing '-overrideIconFile $false', the 'ConvertTo-Icon' function will raise an exception that needs to be catched throw a 'catch' Code Block,
otherwise it'll crash the running PowerShell instance/process.
#> #>
param( [Parameter(Mandatory=$true)] param(
$bitmapPath, [Parameter(Mandatory=$true, position=0)]
$iconPath = "$env:temp\newicon.ico" [string]$bitmapPath,
[Parameter(Mandatory=$true, position=1)]
[string]$iconPath,
[Parameter(position=2)]
[bool]$overrideIconFile = $true
) )
Add-Type -AssemblyName System.Drawing Add-Type -AssemblyName System.Drawing
if (Test-Path $bitmapPath) { if (Test-Path $bitmapPath) {
if ((Test-Path $iconPath) -AND ($overrideIconFile -eq $false)) {
Write-Host "[ConvertTo-Icon] Icon File is found at '$iconPath', and the 'overrideIconFile' Parameter is set to '$overrideIconFile'. Skipping the bitmap to icon convertion..." -ForegroundColor Yellow
return
}
# Load bitmap file into memory, and make an Icon version out of it
$b = [System.Drawing.Bitmap]::FromFile($bitmapPath) $b = [System.Drawing.Bitmap]::FromFile($bitmapPath)
$icon = [System.Drawing.Icon]::FromHandle($b.GetHicon()) $icon = [System.Drawing.Icon]::FromHandle($b.GetHicon())
# Create the folder for the new icon file if it doesn't exists
$iconFolder = (New-Object System.IO.FileInfo($iconPath)).Directory.FullName
[System.IO.Directory]::CreateDirectory($iconFolder) | Out-Null
# Write the Icon File and do some cleaning-up
$file = New-Object System.IO.FileStream($iconPath, 'OpenOrCreate') $file = New-Object System.IO.FileStream($iconPath, 'OpenOrCreate')
$icon.Save($file) $icon.Save($file)
$file.Close() $file.Close()
$icon.Dispose() $icon.Dispose()
#explorer "/SELECT,$iconpath"
} }
else { Write-Warning "$BitmapPath does not exist" } else {
throw [System.IO.FileNotFoundException] "[ConvertTo-Icon] The provided bitmap File Path is not found at '$bitmapPath'."
}
} }
function Copy-Files { function Copy-Files {
<# <#
@ -4808,8 +4874,10 @@ function Invoke-WPFShortcut {
$ArgumentsToSourceExe = "$powershell '$IRM'" $ArgumentsToSourceExe = "$powershell '$IRM'"
$DestinationName = "WinUtil.lnk" $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") { 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 ConvertTo-Icon -bitmapPath "$env:TEMP\cttlogo.png" -iconPath $iconPath
} }
} }
@ -4821,20 +4889,23 @@ function Invoke-WPFShortcut {
$FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk" $FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk"
$FileBrowser.FileName = $DestinationName $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() $FileBrowserResult = $FileBrowser.ShowDialog()
$DialogResultEnum = New-Object System.Windows.Forms.DialogResult $DialogResultEnum = New-Object System.Windows.Forms.DialogResult
if (-not ($FileBrowserResult -eq $DialogResultEnum::OK)) { if (-not ($FileBrowserResult -eq $DialogResultEnum::OK)) {
return return
} }
# Prepare the Shortcut paramter
$WshShell = New-Object -comObject WScript.Shell $WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName) $Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName)
$Shortcut.TargetPath = $SourceExe $Shortcut.TargetPath = $SourceExe
$Shortcut.Arguments = $ArgumentsToSourceExe $Shortcut.Arguments = $ArgumentsToSourceExe
if ($null -ne $iconPath) { if ($iconPath -ne $null) {
$shortcut.IconLocation = $iconPath $shortcut.IconLocation = $iconPath
} }
# Save the Shortcut to disk
$Shortcut.Save() $Shortcut.Save()
if ($RunAsAdmin -eq $true) { if ($RunAsAdmin -eq $true) {