スクリーンキャプチャ・コマンドレット ※200 ミリ秒毎に 1 回画面キャプチャを 60 分間行う。
Param( $WindowTitle ) $src = ' using System; using System.Text; using System.Runtime.InteropServices; public static class H32 { [DllImport(@"User32.dll")] public extern static int GetClassName( IntPtr hWnd, StringBuilder sbClassName, int nMaxCount ); [DllImport(@"User32.dll")] public extern static int GetWindowText( IntPtr hWnd, StringBuilder sbText, int nMaxCount ); [DllImport(@"User32.dll")] public extern static IntPtr GetForegroundWindow(); [DllImport(@"User32.dll")] public extern static IntPtr FindWindow( StringBuilder sbClassName, StringBuilder sbText ); [DllImport(@"User32.dll")] public extern static int GetClientRect( IntPtr hWnd, ref RECT rcClient ); [DllImport(@"User32.dll")] public extern static int ClientToScreen( IntPtr hWnd, ref POINT pt ); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } [StructLayout(LayoutKind.Sequential)] public struct POINT { public int x; public int y; } public static RECT NewRect() { return new RECT(); } public static POINT NewPoint() { return new POINT(); } } ' Add-Type -AssemblyName System.Drawing trap { break } try { Add-Type $src } catch { $Error[0] return } if( $WindowTitle -eq $Null ) { Write-Host 'wrning: 今から 5 秒後にアクティブなウィンドウの情報を取得します。' [System.Threading.Thread]::Sleep( 5000 ) $hWnd = [H32]::GetForegroundWindow() $hWnd.ToString() $sbClassName = [System.Text.StringBuilder]::new( 512 ) [void] [H32]::GetClassName( $hWnd, $sbClassName, $sbClassName.Capacity ) 'クラス名: "{0}"' -f $sbClassName.ToString() $sbWndText = [System.Text.StringBuilder]::new( 512 ) [void] [H32]::GetWindowText( $hWnd, $sbWndText, $sbWndText.Capacity ) 'ウィンドウテキスト: "{0}"' -f $sbWndText.ToString() return } # $hFindWnd = [H32]::FindWindow( $sbClassName, $sbWndText ) # # $hFindWnd # $hFindWnd = [H32]::FindWindow( $sbClassName, $Null ) # # $hFindWnd $hFindWnd = [H32]::FindWindow( $Null, $WindowTitle ) if( $hFindWnd -eq [IntPtr]::Zero ) { Write-Host 'error: ウィンドウを取得できませんでした。' return } $hFindWnd $hWnd = $hFindWnd $rcClient = [H32]::NewRect() [void] [H32]::GetClientRect( $hWnd, [ref]$rcClient ); $ptStart = [H32]::NewPoint() $ptStart.x = $rcClient.Left $ptStart.y = $rcClient.Top [void] [H32]::ClientToScreen( $hWnd, [ref]$ptStart ); $ptEnd = [H32]::NewPoint() $ptEnd.x = $rcClient.Right $ptEnd.y = $rcClient.Bottom [void] [H32]::ClientToScreen( $hWnd, [ref]$ptEnd ); $rc = $rcClient $rc.Left = $ptStart.x $rc.Top = $ptStart.y $rc.Right = $ptEnd.x $rc.Bottom = $ptEnd.y $dt = (Get-Date).ToString( 'yyyy-MM-dd HH:mm:ss' ) # $canvasSize = [System.Drawing.Size]::new( 300, 300 ) $canvasSize = [System.Drawing.Size]::new( $rc.Right - $rc.Left, $rc.Bottom - $rc.Top ) $titleSize = [System.Drawing.Size]::new( 140, 16 ) $ft = [System.Drawing.Font]::new( 'MS ゴシック', 10 ) # $bmp = [System.Drawing.Bitmap]::new( $canvasSize.Width, $canvasSize.Height ) $bmp = [System.Drawing.Bitmap]::new( $rc.Right - $rc.Left, $rc.Bottom - $rc.Top ) [System.Drawing.Graphics] $g = [System.Drawing.Graphics]::FromImage( $bmp ) # 60 * 1000 / 200 for( $i = 0; $i -lt 300; $i ++ ) { $g.CopyFromScreen( $rc.Left, $rc.Top, 0, 0, $canvasSize ) # $X = $canvasSize.Width - $titleSize.Width # $Y = $canvasSize.Height - $titleSize.Height # $g.FillRectangle( [System.Drawing.Brushes]::White, # $X, # $Y, # $titleSize.Width, # $titleSize.Height ) # # $g.DrawString( $dt, $ft, [System.Drawing.Brushes]::Black, $X + 4, $Y + 2 ) $bmp.Save( 'D:\tmp\capture{0:D04}.png' -f ($i + 1), [System.Drawing.Imaging.ImageFormat]::Png ) [System.Threading.Thread]::Sleep( 200 ) } $bmp.Dispose() $g.Dispose()