PowerShell/Scripts/take-screenshot.ps1

38 lines
1.2 KiB
PowerShell
Raw Normal View History

2021-02-09 19:30:45 +01:00
#!/bin/powershell
<#
2021-01-27 10:29:01 +01:00
.SYNTAX ./take-screenshot.ps1 [<directory>]
2021-01-27 10:45:32 +01:00
.DESCRIPTION takes a single screenshot and saves it into the current/given directory
2020-12-29 15:14:21 +01:00
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
2021-02-18 20:17:55 +01:00
param($Directory = "")
2021-01-27 10:29:01 +01:00
2021-01-27 15:12:17 +01:00
function TakeScreenshot { param([string]$FilePath)
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
}
2021-01-27 10:29:01 +01:00
try {
if ($Directory -eq "") {
$Directory = "$PWD"
}
$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-02-16 10:03:20 +01:00
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-01-27 10:29:01 +01:00
exit 1
}