LoginSignup
6
9

More than 5 years have passed since last update.

PowerShell で問題ステップ記録ツールが生成するMHTMLから画像を抽出する

Posted at
  • Windowsには、問題ステップ記録ツール(psr.exe)という、簡易的にスクリーンキャプチャしてくれる便利ツールが標準搭載されている(いつから標準搭載されているのかは知らない)。
  • しかし、出力がZIP化されたMHTMLファイルで、キャプチャ画像はBASE64で符号化されていて、ZIPを解凍しても画像が取り出せない。
  • とりあえず解凍してできたMHTMLファイルから画像(JPEG)を抽出したい。
  • ので、雑に作った。

使い方

PS C:\demo\psr> tree /a /f
フォルダー パスの一覧:  ボリューム TI31063700D
ボリューム シリアル番号は 7884-7485 です
C:.
|   mhtml2jpeg.ps1
|   
\---psr
        Recording_20160924_2126.mht

PS C:\demo\psr> .\mhtml2jpeg.ps1 .\psr\Recording_20160924_2126.mht .\psr
PS C:\demo\psr> 
PS C:\demo\psr> ls .\psr -name
Recording_20160924_2126.mht
screenshot0001.JPEG
screenshot0002.JPEG
screenshot0003.JPEG

コード

Param ([string]$Path, [string]$Destination)

$Path = Resolve-Path $Path
$Destination = Resolve-Path $Destination

$nameRE = [regex]"^Content-Location: (?<Name>screenshot\d{4}.JPEG)$"
$nextRE = [regex]"^--=_NextPart"

$imagePart = $false
$base64 = ""

switch -regex -file $Path
{
    '^$' {}

    $nameRE
    {
        $name = $nameRE.Match($_).Groups['Name'].Value
        $file = Join-Path $Destination $name

        $imagePart = $true
    }

    $nextRE
    {
        if ($imagePart)
        {
            $byte = [Convert]::FromBase64String($base64)
            [IO.File]::WriteAllBytes($file, $byte)

            $imagePart = $false
            $base64 = ""
        }
    }

    default
    {
        if ($imagePart)
        {
            $base64 += $_
        }
    }
}
6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9