Skip to content

Instantly share code, notes, and snippets.

@jamessantiago
Created October 25, 2014 11:45
Show Gist options
  • Save jamessantiago/e77a6b6f2cef6a8679ed to your computer and use it in GitHub Desktop.
Save jamessantiago/e77a6b6f2cef6a8679ed to your computer and use it in GitHub Desktop.
Powershell Screen Recording Scripts
ls D:\Code\PSRec\Screenshots |% {
$thisHash = identify -quiet -format "%#" $_.FullName
if ($thisHash -eq $lastHash) {
rm $_.FullName
write-host "Removed duplicate $($_.FullName)"
}
$lastHash = $thisHash
}
$count = 0
ls D:\Code\PSRec\Screenshots |% {
$count++
write-host "Renaming $($_.FullName)"
ren $_.FullName D:\Code\PSRec\Screenshots\Shot$($count.ToString("0000")).png
}
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter the path and filename")]
[ValidateNotNullorEmpty()]
[ValidateScript({
#verify the folder exists
$folder = Split-Path -Path $_
#Validation differs depending on whether v2 or v3
if ($PSVersionTable.psversion -eq "2.0") {
Test-Path -Path $folder
}
else {
if (! (Test-Path -Path $folder)) {
#write a custom error message for v3
Throw "Can�t verify that $folder exists."
}
else {
$True
}
}
})]
[string]$Path,
[switch]$Full,
[switch]$Passthru
)
If ($host.Runspace.ApartmentState -ne "STA") {
Write-Warning "You must run this in a PowerShell session with an apartment state of STA"
Return
}
#load the necessary assemblies
Add-Type -AssemblyName "System.Drawing","System.Windows.Forms"
if ($Full) {
#capture the full desktop
[Windows.Forms.Sendkeys]::SendWait("{PrtSc}")
}
else {
#capture the current window
[Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
}
#pause enough to give time for the capture to take place
start-sleep -Milliseconds 250
#create bitmap object from the screenshot
$bitmap = [Windows.Forms.Clipboard]::GetImage()
#split off the file extension and use it as the type
[string]$filename=Split-Path -Path $Path -Leaf
[string]$FileExtension= [system.io.path]::GetExtension($Filename)
#get the right format value based on the file extension
Switch ($FileExtension) {
".png" {$FileType=[System.Drawing.Imaging.ImageFormat]::Png}
".bmp" {$FileType=[System.Drawing.Imaging.ImageFormat]::Bmp}
".gif" {$FileType=[System.Drawing.Imaging.ImageFormat]::Gif}
".emf" {$FileType=[System.Drawing.Imaging.ImageFormat]::Emf}
".jpg" {$FileType=[System.Drawing.Imaging.ImageFormat]::Jpeg}
".tiff" {$FileType=[System.Drawing.Imaging.ImageFormat]::Tiff}
".wmf" {$FileType=[System.Drawing.Imaging.ImageFormat]::Wmf}
".exif" {$FileType=[System.Drawing.Imaging.ImageFormat]::Exif}
Default {
Write-Warning "Failed to find a valid graphic file type"
$FileType=$False
}
} #switch
#Save the file if a valid file type was determined
if ($FileType) {
If ($PSCmdlet.ShouldProcess($path)) {
Try {
$bitmap.Save($Path.Trim(),$FileType)
if ($Passthru) {
#write the file object to the pipeline
Get-Item -Path $Path.Trim()
} #if $passthru
} #try
Catch {
Write-Warning "Failed to save screen capture. $($_.Exception.Message)"
} #catch
} #if shouldprocess
} #if $filetype
#clear the clipboard
[Windows.Forms.Clipboard]::Clear()
$recTimer = new-object timers.timer
mkdir D:\Code\PSRec\Screenshots | Out-Null
$global:seq = 0
$takeScreenshot = {
try
{
$global:seq ++
$file = "D:\Code\PSRec\Screenshots\Shot$($global:seq.ToString("0000")).png"
D:\Code\PSRec\New-Screenshot.ps1 -Path $file
}
catch {
write-host $_
}
}
$recTimer.Interval = 500
Register-ObjectEvent -InputObject $recTimer -EventName elapsed –SourceIdentifier recTimer -Action $takeScreenshot | Out-Null
$recTimer.Start() | Out-Null
$recTimer.stop()
Unregister-Event recTimer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment