Skip to content

Instantly share code, notes, and snippets.

@pogin503
Forked from guitarrapc/Get-ScreenShot.ps1
Created March 13, 2017 15:44
Show Gist options
  • Save pogin503/53b5f0b0f60282b6f3d040d0735a3e0d to your computer and use it in GitHub Desktop.
Save pogin503/53b5f0b0f60282b6f3d040d0735a3e0d to your computer and use it in GitHub Desktop.
Screenshot Automation with PowerShell
function Get-ScreenShot
{
[CmdletBinding()]
param(
[parameter(Position = 0, Mandatory = 0, ValueFromPipelinebyPropertyName = 1)]
[ValidateNotNullOrEmpty()]
[string]$OutPath = "$env:USERPROFILE\Documents\ScreenShot",
#screenshot_[yyyyMMdd_HHmmss_ffff].png
[parameter(Position = 1, Mandatory = 0, ValueFromPipelinebyPropertyName = 1)]
[ValidateNotNullOrEmpty()]
[string]$FileNamePattern = 'screenshot_{0}.png',
[parameter(Position = 2,Mandatory = 0, ValueFromPipeline = 1, ValueFromPipelinebyPropertyName = 1)]
[ValidateNotNullOrEmpty()]
[int]$RepeatTimes = 0,
[parameter(Position = 3, Mandatory = 0, ValueFromPipelinebyPropertyName = 1)]
[ValidateNotNullOrEmpty()]
[int]$DurationMs = 1
)
begin
{
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.Windows.Forms
if (-not (Test-Path $OutPath))
{
New-Item $OutPath -ItemType Directory -Force
}
}
process
{
0..$RepeatTimes `
| %{
$fileName = $FileNamePattern -f (Get-Date).ToString('yyyyMMdd_HHmmss_ffff')
$path = Join-Path $OutPath $fileName
$b = New-Object System.Drawing.Bitmap([System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width, [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height)
$g = [System.Drawing.Graphics]::FromImage($b)
$g.CopyFromScreen((New-Object System.Drawing.Point(0,0)), (New-Object System.Drawing.Point(0,0)), $b.Size)
$g.Dispose()
$b.Save($path)
if ($RepeatTimes -ne 0)
{
Start-Sleep -Milliseconds $DurationMs
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment