PowerShell/Scripts/take-screenshots.ps1
2021-01-27 10:32:04 +01:00

58 lines
1.8 KiB
PowerShell
Executable File

#!/snap/bin/powershell
<#
.SYNTAX ./take-screenshot.ps1 [<directory>] [<interval>]
.DESCRIPTION takes screenshots every 60 seconds and saves them into the current/given directory
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param([string]$Directory = "", [int]$Interval = 60)
function GetTimedScreenshots {
[CmdletBinding()] Param(
[Parameter(Mandatory=$True)]
[ValidateScript({Test-Path -Path $_ })]
[string]$Path,
[Parameter(Mandatory=$True)]
[int32]$Interval,
[Parameter(Mandatory=$True)]
[string]$EndTime
)
function GenScreenshot {
$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()
}
Add-Type -Assembly System.Windows.Forms
do {
$Time = (Get-Date)
$FileName = "$($Time.Year)-$($Time.Month)-$($Time.Day)-$($Time.Hour)-$($Time.Minute)-$($Time.Second).png"
[string]$FilePath = (Join-Path $Path $FileName)
write-output "Saving screenshot to $FilePath..."
GenScreenshot
Start-Sleep -Seconds $Interval
} while ((Get-Date -Format HH:%m) -lt $EndTime)
}
try {
if ($Directory -eq "") {
$Directory = "$PWD"
}
GetTimedScreenshots $Directory $Interval "23:59"
exit 0
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}