SCAPaoT

System Center, Automation, Powershell and other Thoughts

Documentation made easy – Convert Problem Step Recorder File to HTML

Since Windows 7 and Server 2008 the build in tool “Problem Step Recorder” can make screenshots automatically on every click that is made. It is perfect for building installation howto’s or any other kind of documentation. Yes, there are more powerfull tools on the market, but hey, its for free… 
Only problem is, the files that are delivered as zipped MHT-Files only. So only browsers can show them. You are not able to import them for editing directly into Microsoft Word.

So I decided to build a little parser script in Powershell that converts the mht files from psr.exe into its html-files and jpeg’s.

This script takes the filepath of the zipfile or the unzipped mht file and extracts the hmtl, css, and jpeg’s into a subfolder:

param($file=$(read-host "filename of psr-zip or psr-mht file? "))

function writefile($dir, $fname, $text)
{
    $text | out-file -append $dir\$fname -Encoding "default"
}

function convertJPG($dir)
{

$jpgfiles = get-item $dir\*.jpeg.txt
foreach($jpg in $jpgfiles)
    {
        $filename = $jpg.name.tostring()

        "$filename -> $($filename.substring(0,$filename.length-4))"

        [System.Convert]::FromBase64String((Get-Content $jpg -readcount 0)) | set-content -Encoding Byte "$dir\$($filename.substring(0,$filename.length-4))"
        remove-item $dir\$filename -force
    }   
}

function extractPSR($zipfile, $destfolder)
{
 $shellApplication = new-object -com shell.application
 $zip = $shellApplication.Namespace($zipfile)
 $dest = $shellApplication.Namespace($destfolder)
 $dest.copyhere($zip.Items())
}
if(test-path $file)
{
$file = get-item $file

$folder = (get-date -Format "yyyyMMddHHmmss").tostring()
$folderObject = new-item $folder -type directory -force
$filename = ""

if($file.name.tostring().tolower().endswith(".zip"))
{
 $unzipdest = "$($folderobject.fullname.tostring())\temp"
 new-item $unzipdest -type directory -force | out-null
 extractPSR $file.fullname $unzipdest
 $psrfile = get-item "$unzipdest\*.mht"
}
else
{
 $psrfiles = $file
}

$content = get-content $psrfile

"Start: creating files in folder $pwd\$folder"

foreach($line in $content)
{
  switch -wildcard ($line)
  {
    "Content-Location: *"
    {
        #$line
        $filename = $line.split(":")[1].trim()
         if($filename.tolower().endswith(".jpeg"))
         {
            "writing: $filename.txt"
         }
         else
         {
            "writing: $filename"
         }

        break;
    }
    "--=_NextPart_*" { break; }

    "Content-Type: *" { break; }

    "Content-Transfer-Encoding: base64" { break; }

    default
    {
        if($filename -ne "")
        {
            if($filename.tolower().endswith(".jpeg"))
            {
                if($line -ne "")
                {
                    writefile $folder "$filename.txt" $line
                }
            }
            else
            {

                writefile $folder $filename $line
            }
        }
        break
    }

  }

}
"Finished: Creating files"
"Start: converting pictures from text to JPG"

convertJPG $folder
"Finished: converting pictures"

$yesno = read-host "Open containing folder? [y] "

if($yesno -eq "" -or $yesno.tolower() -eq "y")
{
    &explorer.exe $pwd\$folder
}
}
else
{
    "ERROR: $pwd\$file not found"
}

Next thougts are to convert it into a standard documentation directly or crop the slideshow of.

But these are plans for the future,
as well as adding some more comments to the code ;-)

Update

As there are several errors with the linefeeds while copying the source code, here you can download it as a .zip-File

psr.zip

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

COMMENTS