Tagged: WinPE

Add text to images with PowerShell

Been working on some server deployment lately and had some problems with the WinPE… so to not get to confused when using different PE’s to boot up the servers we added some image-information on the background.

Did that in MS Paint… but how fun is that? 😛

Here is a cool function that add some text to a image (Original code from http://www.ravichaganti.com/blog/?p=1012)

Function AddTextToImage {
	# Orignal code from http://www.ravichaganti.com/blog/?p=1012
	[CmdletBinding()]
	PARAM (
		[Parameter(Mandatory=$true)][String] $sourcePath,
		[Parameter(Mandatory=$true)][String] $destPath,
		[Parameter(Mandatory=$true)][String] $Title,
		[Parameter()][String] $Description = $null
	)

	Write-Verbose "Load System.Drawing"
	[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
	
	Write-Verbose "Get the image from $sourcePath"
	$srcImg = [System.Drawing.Image]::FromFile($sourcePath)
	
	Write-Verbose "Create a bitmap as $destPath"
	$bmpFile = new-object System.Drawing.Bitmap([int]($srcImg.width)),([int]($srcImg.height))

	Write-Verbose "Intialize Graphics"
	$Image = [System.Drawing.Graphics]::FromImage($bmpFile)
	$Image.SmoothingMode = "AntiAlias"
	
	$Rectangle = New-Object Drawing.Rectangle 0, 0, $srcImg.Width, $srcImg.Height
	$Image.DrawImage($srcImg, $Rectangle, 0, 0, $srcImg.Width, $srcImg.Height, ([Drawing.GraphicsUnit]::Pixel))

	Write-Verbose "Draw title: $Title"
	$Font = new-object System.Drawing.Font("Verdana", 24)
	$Brush = New-Object Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(255, 0, 0,0))
	$Image.DrawString($Title, $Font, $Brush, 10, 10)
	
	if ($Description -ne $null) {
		Write-Verbose "Draw description: $Description"
		$Font = New-object System.Drawing.Font("Verdana", 12)
		$Brush = New-Object Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(120, 0, 0, 0))
		$Image.DrawString($Description, $Font, $Brush, 10, 50)
	}

	Write-Verbose "Save and close the files"
	$bmpFile.save($destPath, [System.Drawing.Imaging.ImageFormat]::Bmp)
	$bmpFile.Dispose()
	$srcImg.Dispose()
}

With that piece of code and some slightly modified functions from Michael Niehaus you can do some cool stuff…

$taskSequence = "Server Deployment Task Sequence"

Import-Module Misc\Image-Functions.psm1
Import-Module SCCM\SCCM-Functions.psm1

$sccm = Connect-SCCMServer
$taskSequence = Get-TaskSequencePackage -SccmServer $sccm -filter "Name = '$taskSequence'"
$BootImage = Get-BootImagePackage -SccmServer $sccm -filter "PackageID = '$($taskSequence.BootImageID)'"
$DescriptionText = "Version:`t$($BootImage.Version)`nPackageID:`t$($BootImage.PackageID)`n`n$($BootImage.Description)"

AddTextToImage -sourcePath "X:\Path\WinPE-Background-Source.bmp" -destPath "X:\Path\WinPE-Background-To-Inject.bmp" -Title $BootImage.Name -Description $DescriptionText

Shouldn’t be that hard to auto-inject the BMP to the WIM file…