Author Posts

May 4, 2018 at 9:42 pm

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)

$bitmap.Save($File) 
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.To.Add($SendTo)
$Message.Subject = $EmailSubject
$Message.Body = $body
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att)
$smtp.Send($Message)
$attachment.Dispose()

May 7, 2018 at 11:55 am

Hi

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

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1

Regards

Jake

May 7, 2018 at 2:16 pm

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

May 7, 2018 at 3:23 pm

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.