Public/New-IntuneWin32AppIcon.ps1
function New-IntuneWin32AppIcon { <# .SYNOPSIS Converts a PNG/JPG/JPEG image file available locally to a Base64 encoded string. .DESCRIPTION Converts a PNG/JPG/JPEG image file available locally to a Base64 encoded string. .PARAMETER FilePath Specify an existing local path to where the PNG/JPG/JPEG image file is located. .NOTES Author: Nickolaj Andersen Contact: @NickolajA Created: 2020-01-04 Updated: 2020-01-04 Version history: 1.0.0 - (2020-01-04) Function created #> [CmdletBinding(SupportsShouldProcess = $true)] param( [parameter(Mandatory = $true, HelpMessage = "Specify an existing local path to where the PNG/JPG/JPEG image file is located.")] [ValidateNotNullOrEmpty()] [ValidatePattern("^[A-Za-z]{1}:\\\w+\\\w+")] [ValidateScript({ # Check if path contains any invalid characters if ((Split-Path -Path $_ -Leaf).IndexOfAny([IO.Path]::GetInvalidFileNameChars()) -ge 0) { Write-Warning -Message "$(Split-Path -Path $_ -Leaf) contains invalid characters"; break } else { # Check if file extension is PNG/JPG/JPEG $FileExtension = [System.IO.Path]::GetExtension((Split-Path -Path $_ -Leaf)) if (($FileExtension -like ".png") -or ($FileExtension -like ".jpg") -or ($FileExtension -like ".jpeg")) { return $true } else { Write-Warning -Message "$(Split-Path -Path $_ -Leaf) contains unsupported file extension. Supported extensions are '.png', '.jpg' and '.jpeg'"; break } } })] [string]$FilePath ) # Handle error action preference for non-cmdlet code $ErrorActionPreference = "Stop" try { # Encode image file as Base64 string $EncodedBase64String = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("$($FilePath)")) Write-Output -InputObject $EncodedBase64String } catch [System.Exception] { Write-Warning -Message "Failed to encode image file to Base64 encoded string. Error message: $($_.Exception.Message)" } } |