2021-04-21 19:53:52 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
|
|
|
|
take-screenshot.ps1 [<directory>]
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Takes a single screenshot and saves it into the current/given directory
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> .\take-screenshot.ps1 C:\Temp
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
2021-08-03 15:53:57 +02:00
|
|
|
|
Author: Markus Fleschutz
|
|
|
|
|
License: CC0
|
2020-10-03 23:26:33 +02:00
|
|
|
|
#>
|
|
|
|
|
|
2021-07-15 15:51:22 +02:00
|
|
|
|
param([string]$Directory = "$PWD")
|
2021-01-27 10:29:01 +01:00
|
|
|
|
|
2021-01-27 15:12:17 +01:00
|
|
|
|
function TakeScreenshot { param([string]$FilePath)
|
2021-01-27 10:44:30 +01:00
|
|
|
|
Add-Type -Assembly System.Windows.Forms
|
|
|
|
|
$ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen
|
|
|
|
|
$ScreenshotObject = New-Object Drawing.Bitmap $ScreenBounds.Width, $ScreenBounds.Height
|
|
|
|
|
$DrawingGraphics = [Drawing.Graphics]::FromImage($ScreenshotObject)
|
|
|
|
|
$DrawingGraphics.CopyFromScreen( $ScreenBounds.Location, [Drawing.Point]::Empty, $ScreenBounds.Size)
|
|
|
|
|
$DrawingGraphics.Dispose()
|
|
|
|
|
$ScreenshotObject.Save($FilePath)
|
|
|
|
|
$ScreenshotObject.Dispose()
|
2021-01-27 10:29:01 +01:00
|
|
|
|
}
|
2020-10-03 23:26:33 +02:00
|
|
|
|
|
2021-01-27 10:29:01 +01:00
|
|
|
|
try {
|
2021-01-27 10:44:30 +01:00
|
|
|
|
$Time = (Get-Date)
|
|
|
|
|
$Filename = "$($Time.Year)-$($Time.Month)-$($Time.Day)-$($Time.Hour)-$($Time.Minute)-$($Time.Second).png"
|
|
|
|
|
$FilePath = (Join-Path $Directory $Filename)
|
|
|
|
|
|
|
|
|
|
write-output "Saving screenshot to $FilePath..."
|
|
|
|
|
TakeScreenshot $FilePath
|
2021-01-27 10:29:01 +01:00
|
|
|
|
exit 0
|
|
|
|
|
} catch {
|
2021-05-02 21:30:48 +02:00
|
|
|
|
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-01-27 10:29:01 +01:00
|
|
|
|
exit 1
|
2020-10-03 23:26:33 +02:00
|
|
|
|
}
|