Clicky

20230212

Make screenshot and send to email

The next Powershell script makes a screenshot and sends it to your email address by pressing the Control + Alt + P keys simultaniously.

Some highlights:

  • Secure: SMTP account password is bound to USERPROFILE and is never stored in plaintext.
  • Supported email servers: Google, Microsoft (SMTP and Exchange) and other SMTP servers. 
  • Enterprise desktops: The installer will store the script and the encoded password in %LOCALAPPDATA%, so it will work in most enterprise Windows desktops.
  • Install: The script has an installation procedure and password set procedure build-in.

Installation:

  • Store the Powershell code in some location (e.g.: C:\Temp\psEmailScreenshot.ps1)
  • To install the "psEmailScreenshot" script, run the next command:

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

  • An icon is created on the Desktop:

 

  • To set (or change) the SMTP sender's password, issue this command:

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

How it works: Press: Ctrl+Alt+P keys simultaneously and click "Yes" in the Yes/No dialog. The dialog is shown for 10s. When the "No" button is pressed or when no button pressed, the screenshot will be discarded.



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

$MailTo           = "yourEmailAddress@yourEmailDomain.com"

$yesNoDialog      = $true


#--- MAIN ------------------------------------------------------------------------------------------

if (!(test-path $scriptFolder)) {New-Item $scriptFolder -ItemType Directory | out-null}


#--- Install the script and create a shortcut on the desktop...
if ($($args[0]).toLower() -EQ "install")
{
    #--- Copy the PS file to $home and create a shorcut with HotKey...
    copy-item $scriptFilename "$scriptFolder\$scriptName" -force | out-null

    #--- Create shortcut on desktop...
    $iconFile = "$home\Desktop\Email Screenshot.lnk"
    $hotkey                = "CTRL+ALT+P"
    $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       = $hotkey
    $shortcut.WindowStyle  = 7 #--- 7 => hidden
    $Shortcut.Save()

    write-host "`n*** $scriptName is installed. Keyboard shortcut: $hotkey"
   
    Exit

}


#--- Store the SMTP sender's password as SecureString. Note: this password is bound to the Windows Account of the logged on user...
if ($($args[0]).toLower() -EQ "password")
{

    $password = Read-Host -assecurestring "Enter sender's email 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 a screenshot...
$screenPropertiesshotFilename = "$($env:temp)\" + $env:computername + "_" + $env:username + "_" + "$((get-date).tostring("yyyy.MM.dd-HH.mm.ss")).png"

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);
$cursorBounds     = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position, [System.Windows.Forms.Cursor]::Current.Size)
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds)


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

#--- Email the screenshot file...
$imageObject.Save($screenPropertiesshotFilename, [System.Drawing.Imaging.ImageFormat]::Png)
$MailSubject    = "Screenshot from computer: $($env:computername), user: $($env:username)"
$Attachment     = $screenPropertiesshotFilename


#--- Hotmail...
    # $SmtpServer  = "smtp-mail.outlook.com"
    # $MailFrom    = "yourEmailAddress@hotmail.com"
    # $smtpUser    = $MailFrom

    # $Credentials    = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $smtpUser, (Get-Content $smtpPasswordFile | ConvertTo-SecureString)
    # Send-MailMessage -To "$MailTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials -Attachments $Attachment


#--- Gmail...
    # $SmtpServer = "smtp.gmail.com"
    # $MailFrom   = "yourEmailAddress@gmail.com"

    # # The password is an app-specific password, you MUST have 2-factor-auth enabled
    # $Password = "dcobnrrnmvrfyuex" | ConvertTo-SecureString -AsPlainText -Force
    # $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $MailFrom, $Password
    # Send-MailMessage -From $MailFrom -To $mailTo -Subject $MailSubject -SmtpServer $smtpServer -port 587 -UseSsl -Credential $Credential -Attachments $Attachment


#--- Exchange/Outlook...
    # $outlook = new-object -comobject outlook.application
    # $email = $outlook.CreateItem(0)

    # $email.To = "yourEmail@someDomain.com"
    # $email.Subject = $MailSubject
    # $email.Body = " "
    # $email.Attachments.add($Attachment)
    # $email.Send()

    # $outlook.Quit()


#--- hMailserver...
    $SMTPServer       = "mail.yourEmailDomain.com"
    $smtpUser         = "smtp@yourEmailDomain.com"
    $mailFrom         = $smtpUser
    [INT]$SMTPport    = 587 #--- STARTTLS...
    $Credentials    = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $smtpUser, (Get-Content $smtpPasswordFile | ConvertTo-SecureString)
    Send-MailMessage -To $MailTo -from $MailFrom -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -port $SMTPport -Credential $Credentials -Attachment $Attachment

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

start-sleep 10


No comments :

Post a Comment

Real Time Web Analytics