Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active July 31, 2023 15:21
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save guitarrapc/9870497 to your computer and use it in GitHub Desktop.
Save guitarrapc/9870497 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
}
}
}
}
@rajeevupadhyay2007
Copy link

Hi Team,

I am looking for a power shell script that ping several servers and take the screen shot of ping response and then save it in a word file .

@jurcacristian
Copy link

jurcacristian commented Mar 7, 2018

Hi Guys, looking for something like this and then email the screenshot. Now since everytime i take a screenshot, the file is different, how can i use that output for my input when sending the email ? Don't know that much in PShell but i guess we can make it a variable, somehow ?
example of screenshot output file:
screenshot_20180307_042959_2654.png
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment