Alternatives to storing image data in script as Base64 string?

DaveK 1,871 Reputation points
2023-03-01T16:07:20.8333333+00:00

Hi,

So after much head scratching and searching for a script which seems to have gone walkabout, it turns out its a false positive detection in my AV which has caused the issue and deleted my script! I'll be able to put an exception in the AV for the file, restore it from backup etc so in this instance it isn't causing a problem know I know about it but its got me thinking on if it happens again.

The script in question emails its output to a few users and I'm using a HTML body to generate dynamic content depending on the script result and I've included an image as as header in keeping with company branding etc - all worked fine for years. It turns out it seems to be the image which is encoder in Base64 that causes the issue as with the $headerfile = "base64#1234hjgetr etc" removed, the detection stops and the AV leaves me alone again.

I've had a quick web search for if using Base64 to encode images to string is the best way forward and a few sources has said possibly not so I was wondering what others think and if there are any different approaches I can take. My goal is to have the script contain the image so I'm only ever needing to move the one ps1 file around and from a code point of view, I can just copy the email function out of the script to re-use knowing it will always have the right corporate feel to it without having to start from scratch.

Does anyone has any suggestion on how I can encode images to be contained within the script to avoid having to copy image files alongside the script and load from storage? I don't want to upload to cloud or share the images from a central share.

cheers

Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Aung Zaw Min Thwin 306 Reputation points
    2023-04-24T03:43:18.19+00:00

    pls refer to below sample: Hope that helps :)

    $Path = c:\temp\abc.jpg
    $path = (Get-Item $Path).FullName
    $ByteArr = [io.file]::ReadAllBytes($Path)
    
    $LineLength = 80
    $base64Str = [Convert]::ToBase64String($ByteArr) -replace ".{$LineLength}" , "$&`r`n"
    
    #to convert from base64:
    #$ByteArr1 = [convert]::FromBase64String($base64Str)
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.