0

I am working on making a powershell script with a png file and an html file in it,is it possible to process these files in the PowerShell script or something similar so that I can eventually convert it to one Single html file and also if possible that script reduce the size of html file too.

My script as below which I took from google, I have structed up here

$PSScriptRoot = "Inputpngimagepath" $FinalOutput = "outputhtmlfilepath"

Get-Content "$PSScriptRoot\vCenter_CPU_Usage.png" -Raw -Encoding Byte | ConvertTo-DeflateBase64String | Set-Clipboard 
$myvar = "$FinalOutput" 
ConvertFrom-DeflateBase64String $myvar -OutputFormat Byte | Set-Content *.png -Encoding Byte -NoNewline

I want to combine both the html and png files into a single offline html file so that I can send it as an attachment. I need to send about 21 html files in a single email as a html file. I have health check report html files.(ie) The user must click on each of the 21 HTML files in order to view the data, including the charts.

8
  • You deleted your previous question. Do not do that. Editing your questions to improve them and resolve issues with them. Please read the HELP section to better understand how this site works. superuser.com/questions/1841022/… Commented May 1 at 16:50
  • It was closed, so i thought no one will reply to closed thread
    – DINU
    Commented May 1 at 16:54
  • It was closed because it needed more information. Once more information is added the question can be resubmitted to the community to be opened. These instructions are given to you at the top of the question in the message that informed you the question is closed. I strongly recommend that you read those instructions thoroughly, and that you read the Help section as well, to understand how this site works and avoid making these errors in the future. Commented May 1 at 16:57
  • You show a script you got from a web search. Have you tried it? What happens? Commented May 1 at 16:59
  • 1
    And I believe you are still not ready to use this website. If there's an error, not including it in the question is definitely far from the best move.
    – Destroy666
    Commented May 2 at 12:19

1 Answer 1

0
param(
  [Parameter(Mandatory=$true)]
  [string] $FilePath
)

$imageFile = Get-Item $FilePath
if ($imageFile -eq $null) {
  Write-Error "File not found: $FilePath"
  exit 1
}

$imageData = Get-Content $FilePath -Encoding Byte

$base64EncodedData = [Convert]::ToBase64String($imageData)
$contentType = Resolve-ContentType -Path $FilePath

Write-Output "<img src='data:$contentType;base64,$base64EncodedData' />"

Save to: image-to-base64.ps1
Run it: .\image-to-base64.ps1 "C:\path\to\image.jpg"

4
  • Do I need to mention $FilePath = "Imagefolderpath" in the script ? because I am getting error : Resolve-ContentType : The term 'Resolve-ContentType' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At "folder path".ps1:16 char:16 + $contentType = Resolve-ContentType -Path $FilePath + ~~~~~~~~~~~~~~~~~~~+ CategoryInfo : ObjectNotFound: (Resolve-ContentType:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
    – DINU
    Commented May 2 at 17:41
  • FYI : I have hidden the "folder path",and also how can I add mutliple images in the script to get converted ?
    – DINU
    Commented May 2 at 17:41
  • #Embed png images into html single $images = Get-ChildItem "$OutputPath\vCenter_CPU_Usage.png" $ImageHTML = $images | % { $ImageBits = [Convert]::ToBase64String((Get-Content $_ -Encoding Byte)) "<img src=data:image/png;base64,$($ImageBits) alt='vCenter_CPU_Usage.png'/>" } $tab3 += "<img src=data:image/png;base64,$($ImageBits) alt='vCenter_CPU_Usage.png'/>" This worked for me, I have inserted in my script...... Thanks
    – DINU
    Commented May 2 at 18:47
  • @DINU Awesome! I knew it would get you on the right track. You should accept it as a working solution. :)
    – JayCravens
    Commented May 2 at 23:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .