Clicky

20240330

psEmailScreenshot.ps1: Email a screenshot

The next script takes a screenshot and sends the screenshot as PNG file to your emailaddress.

You need to "install" the script. The installation will copy the script to the user's "appdata" folder. This allows the script also to work on restricted desktops, e.g., on managed desktops. 

Also, you need to enter the email sender account password. The password will be stored encoded in the same folder as the script. There is no plaintext password shown or stored, no other user can (re-)use this password file, and copying the password file to another PC will not work.

 

Install:

C:\Temp\> powershell -file psEmailScreenshot.ps1 install

The installation will create a shortcut on your desktop:


The script can (also) be invoked with the shortcut keys: CTRL+ALT+S

 

Set password:

C:\Temp\> powershell -file psEmailScreenshot.ps1 setpassword

 

The complete setup procedure will look this:





 

 

 


The script: psEmailScreenshot.ps1:

#--- User variables...
$SMTPServer       = "{IP or FQDN to mailserver}"
[INT]$SMTPport    = 587 #--- STARTTLS
$smtpUser         = "{emailSenderAccount@domain.ext}"
$EmailFrom        = "{emailscreenshot@domain.ext}"
$EmailTo          = "{receiver@domain.ext}"

#--- MAIN ------------------------------------------------------------------------------------------
if ($args.count -NE 1) {

    Break

}

$scriptFilename   = $MyInvocation.MyCommand.Path
$scriptName       = $MyInvocation.MyCommand
$scriptFolder     = "$home\appdata\Local\mailScreenshot"
$smtpPasswordFile = "$scriptFolder\EmailPassword.txt"

if (!(test-path $scriptFolder)) {

    New-Item $scriptFolder -ItemType Directory | out-null

}


if ($($args[0]).toLower() -EQ "install")
{

    #--- Copy the PS file to %appdata% and create a Shortcut on the Desktop with a HotKey...
    copy-item $scriptFilename "$scriptFolder\$scriptName" -force | out-null

    #--- Create shortcut...
    $iconFile              = "$home\Desktop\Email Screenshot.lnk"
    $wsh                   = New-Object -comObject WScript.Shell
    $Shortcut              = $wsh.CreateShortcut($iconFile)
    $Shortcut.TargetPath   = "powershell"
    $shortcut.Arguments    = " -file $scriptFolder\$scriptName emailscreenshot"
    $shortcut.IconLocation = "SHELL32.dll,24"
    $shortcut.HotKey       = "CTRL+ALT+S"
    $shortcut.WindowStyle  = 7 #--- 7 => hidden
    $Shortcut.Save()
   
    write-host "*** Shortcut: '$iconFile'"
    write-host "*** Keyboard shortcut: CTRL+ALT+S"
    write-host "*** $scriptName is installed."

    Exit

}

#--- Store the SMTP sender's password as SecureString. Note: this password is bound to the %USERPROFILE%...
if ($($args[0]).toLower() -EQ "setpassword") {

    $password = Read-Host -assecurestring "Enter sender email account's password now"
    $password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
    $password | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File $smtpPasswordFile
    $password = $null

    Exit

}

#--- Take screenshot...
Add-Type -AssemblyName System.Windows.Forms
$screenProperties = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$imageObject      = New-Object System.Drawing.Bitmap($screenProperties.Width, $screenProperties.Height)
$graphic          = [System.Drawing.Graphics]::FromImage($imageObject)
$point            = New-Object System.Drawing.Point(0, 0)
$graphic.CopyFromScreen($point, $point, $imageObject.Size);

#--- Yes/No dialog...
$wshell = New-Object -ComObject Wscript.Shell
$answer = $wshell.Popup("Email screenshot.",10,"Send screenshot to: $EmailTo ?",4+32)

if ($answer -EQ 6) {

    #--- Convert Bitmap to PNG and store in %TEMP%...
    $screenPropertiesshotFilename = "$($env:temp)\" + $env:computername + "_" + $env:username + "_" + "$((get-date).tostring('yyyy.MM.dd-HH.mm.ss')).png"
   
    #--- Bitmap memory to PNG: https://stackoverflow.com/questions/3517965/convert-bmp-to-png-in-memory-for-clipboard-pasting-in-net
    $imageObject.Save($screenPropertiesshotFilename, [System.Drawing.Imaging.ImageFormat]::Png)

    #--- Email the screenshot file..
    $MailSubject    = "Screenshot from computer: $($env:computername), user: $($env:username)"
    $Credentials    = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $smtpUser, (Get-Content $smtpPasswordFile | ConvertTo-SecureString)
    Send-MailMessage -To "$emailTo" -from "$emailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials -Attachment $screenPropertiesshotFilename

    #--- Cleanup...
    remove-item $screenPropertiesshotFilename -force

}



Note: the "Send-MailMessage" method is depricated. See here for additional information: https://github.com/dotnet/platform-compat/blob/master/docs/DE0005.md

 

No comments :

Post a Comment

Real Time Web Analytics