#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
Toaddress = itsm.getParameter("Enter_to_Address")        # Enter the email id where you want to send screenshot. Type as STRING.
SMTPUsername = itsm.getParameter("Enter_SMTP_Username")  # Enter your Gmail address. Type as STRING.
SMTPPassword = itsm.getParameter("Enter_SMTP_Password")  # Enter your Gmail password. Type as STRING.

import os
import ctypes
import uuid

ps_filename = 'Take-ScreenShot-' + str(uuid.uuid4()) + '.ps1'
file = 'C:\\ProgramData\\Xcitium\\' + ps_filename
Toaddress = '"' + Toaddress + '"'

ps_script = """
$ErrorActionPreference = "Stop"
#############################################################################
# Capturing a screenshot
#############################################################################
$timestamp = (Get-Date -Format yyyy-MM-dd-HHmmss)
$computer  = $env:COMPUTERNAME
$user      = $env:USERNAME
$folder    = "C:\\ProgramData\\Xcitium\\Screenshots"
New-Item -ItemType Directory -Path $folder -Force | Out-Null
$bmp = "$folder\\$computer-$user-$timestamp.bmp"
$jpg = "$folder\\$computer-$user-$timestamp.jpg"

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$screen   = [System.Windows.Forms.SystemInformation]::VirtualScreen
$bitmap   = New-Object System.Drawing.Bitmap $screen.Width, $screen.Height
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen($screen.Left, $screen.Top, 0, 0, $bitmap.Size)
$bitmap.Save($bmp)
$graphics.Dispose()
$bitmap.Dispose()

$image = [System.Drawing.Image]::FromFile($bmp)
$image.Save($jpg, [System.Drawing.Imaging.ImageFormat]::Jpeg)
$image.Dispose()
Remove-Item $bmp -Force

Write-Output "Screenshot saved to:"
Write-Output $jpg

function do_mail {

$SMTPServer = "smtp.gmail.com"
$SMTPPort   = 587
$Username   = %s
$Password   = %s
$To         = %s
$time       = Get-Date

$subject = "Screenshot of $computer on $time"
$body = @"
<html>
<body>
<p>Please find the screenshot of <b>$computer</b> taken on <b>$time</b> below.</p>
<img src="cid:$computer-$user-$timestamp.jpg">
<br><br>
<p><i>If the screenshot is not visible in this email due to image blocking settings,
you can find it at the following location on the endpoint:</i></p>
<p><b>$jpg</b></p>
</body>
</html>
"@

$attachment = New-Object System.Net.Mail.Attachment -ArgumentList $jpg
$attachment.ContentDisposition.Inline = $True
$attachment.ContentDisposition.DispositionType = "Inline"
$attachment.ContentType.MediaType = "image/jpeg"
$attachment.ContentId = "$computer-$user-$timestamp.jpg"

$message = New-Object System.Net.Mail.MailMessage
$message.IsBodyHTML = $true
$message.Subject = $subject
$message.Body = $body
$message.To.Add($To)
$message.From = $Username
$message.Attachments.Add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Send($message)
$attachment.Dispose()
$message.Dispose()
Write-Output "Email sent to $To"
}

do_mail
""" % (SMTPUsername, SMTPPassword, Toaddress)

# Create Xcitium folder if it doesn't exist
if not os.path.exists('C:\\ProgramData\\Xcitium'):
    os.makedirs('C:\\ProgramData\\Xcitium')

try:
    fobj = open(file, "w")
    fobj.write(ps_script)
    fobj.close()
except Exception as e:
    print "Error writing PowerShell script: " + str(e)
    raise

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert  = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

try:
    with disable_file_system_redirection():
        out = os.popen(
            r'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\ProgramData\Xcitium\{}"'.format(ps_filename)
        ).read()
    print out
except Exception as e:
    print "Error executing PowerShell script: " + str(e)
    raise
finally:
    if os.path.exists(file):
        os.remove(file)
        print "Temp PowerShell script cleaned up."