Script to take screenshot and send email

Welcome Forums General PowerShell Q&A Script to take screenshot and send email

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #100261

    Topics: 3
    Replies: 2
    Points: 0
    Rank: Member

    Hi. I'm new to PS. I'm trying to run this script which will launch an application in a server, take a screenshot and send an email. I can RDP in to the server and run the script and it would work. But i would like to run this automatically using task scheduler or something to run it every 5/6 hours(I know its a dumb question, as background processes in windows don't work that way). Below is something that im trying to run. Any idea that can lead me to the correct path would be greatly appreciated. I'm hoping this can be done without any third party tool, not that i cant

    start-process "PATH TO AN APPLICATION.EXE"
    Start-Sleep -Milliseconds 1000
    $File = ".\SCREENSHOT.bmp"
    Add-Type -AssemblyName System.Windows.Forms
    Add-type -AssemblyName System.Drawing
    $Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
    $Width = $Screen.Width
    $Height = $Screen.Height
    $Left = $Screen.Left
    $Top = $Screen.Top
    $bitmap = New-Object System.Drawing.Bitmap $Width, $Height
    $graphic = [System.Drawing.Graphics]::FromImage($bitmap)
    $graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)
    Write-Output $File
    $SendTo = "sENDTO"
    $SMTPServer = "smtp" 
    $EmailFrom = “ABC@ABC.COM”
    $EmailSubject = “SCREENSHOT”
    $Image = ".\SCREENSHOT.bmp"
    $Message = new-object Net.Mail.MailMessage
    Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
    $attachment = new-object Net.Mail.Attachment($Image)
    $attachment.ContentId = "att"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $body = ''
    $Message.From = $EmailFrom
    $Message.Subject = $EmailSubject
    $Message.Body = $body
    $Message.IsBodyHTML = $true
  • #100306

    Topics: 0
    Replies: 61
    Points: -11
    Rank: Member


    Is it possible to use Send-Mailmessage? This is atleast in my opinion better choice.



    • #100312

      Topics: 3
      Replies: 2
      Points: 0
      Rank: Member

      Thanks Jake! I tried Send-Mailmessage as well. I'm getting an email if i run it with task scheduler but it just sends a blank email without the screenshot and i see the process running at the background. Hoping if there is a way to accomplish this

    • #100320

      Topics: 1
      Replies: 1358
      Points: 1,813
      Helping Hand
      Rank: Community Hero

      If I remeber right there is no screen to take a shot of it if there is no interactive user session. So I doupt that it is possible without some quirky third party tool to do what you need, sorry.

The topic ‘Script to take screenshot and send email’ is closed to new replies.