PowerShell/Scripts/take-screenshots.ps1

58 lines
1.8 KiB
PowerShell
Raw Normal View History

2020-10-03 23:32:24 +02:00
#!/snap/bin/powershell
<#
2021-01-27 10:32:04 +01:00
.SYNTAX ./take-screenshot.ps1 [<directory>] [<interval>]
.DESCRIPTION takes screenshots every 60 seconds and saves them 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-01-27 10:32:04 +01:00
param([string]$Directory = "", [int]$Interval = 60)
function GetTimedScreenshots {
[CmdletBinding()] Param(
[Parameter(Mandatory=$True)]
[ValidateScript({Test-Path -Path $_ })]
2021-01-27 10:32:04 +01:00
[string]$Path,
[Parameter(Mandatory=$True)]
2021-01-27 10:32:04 +01:00
[int32]$Interval,
[Parameter(Mandatory=$True)]
2021-01-27 10:32:04 +01:00
[string]$EndTime
)
2021-01-27 10:32:04 +01:00
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()
}
2021-01-27 10:32:04 +01:00
Add-Type -Assembly System.Windows.Forms
2021-01-27 10:32:04 +01:00
do {
$Time = (Get-Date)
$FileName = "$($Time.Year)-$($Time.Month)-$($Time.Day)-$($Time.Hour)-$($Time.Minute)-$($Time.Second).png"
[string]$FilePath = (Join-Path $Path $FileName)
2021-01-27 10:32:04 +01:00
write-output "Saving screenshot to $FilePath..."
GenScreenshot
2021-01-27 10:32:04 +01:00
Start-Sleep -Seconds $Interval
} while ((Get-Date -Format HH:%m) -lt $EndTime)
}
2021-01-27 10:32:04 +01:00
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
}