# ///////////////////////////////////
# ////////////// NOTES //////////////
# ///////////////////////////////////
#	
#	Reverting increased delay because of timing issues that need to be solved having to do with increased runtime. Causes script to run longer than expected and alters time per program
#	----Increased keystroke delay to 180ms as per agreement with Intel, via Mike Ash
#
#    Converted all paths to be relative to the Productivity.ps1 file. This should allow Rundown to use
#        the same scripts as the DAQ systems
#    
#    Added $ReplyAddress param to specify a different set of address to reply to as per PLE request.
#        ReplyAddress will default to $Address if not specified to maintain compatability with older setups.
#
#        Version #:        1.0.5
# 
# 
# ///////////////////////////////////
# ///////////////////////////////////
# ///////////////////////////////////

param(  [Int]$RunTime = 1000,
        [switch]$Debug,
        [switch]$screens,
        [switch]$Random,
        [switch]$Trace,
        [string]$TracePath = ".",
        [string]$Address = "",
        [string]$ReplyAddress = $Address,
        [double]$SleepFactor = 1,
        [switch]$OneNoteUWP,
        [string]$OneNotePageLink = ""
)

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# ///////////////////////////////////
# ////////////// Vars ///////////////
# ///////////////////////////////////

$Global:Keycount = 0

$sw = [Diagnostics.Stopwatch]::StartNew()
$ExcelFileName = "TestBook.xlsx"
$PPTFileName = "sample.pptx"
$DocFileName = "test.docx"
$TextFilename = "WordContent.txt"
$CurrentPath = (split-path -parent $MyInvocation.MyCommand.Definition)   #Execution Location
$FilesPath = $CurrentPath + "\content\"   #Resource files location	# Assesment changes

#if($scripts -eq $null){$scripts = (split-path -Parent (split-path -parent $FilesPath)) + "\scripts"}
$scripts = Join-Path $PSSCriptRoot "scripts"	# Assesment changes

if ($TracePath -eq ".") {
    $TracePath = $CurrentPath
}
if (!(Test-Path $TracePath)) {
    Write-Host "Trace path $TracePath doesn't exist.  Creating."
    New-Item -ItemType Directory -Force -Path $TracePath
}

if (!$Random) {
    # Set random seed to constant, for life of powershell session.
    Get-Random -setSeed 0x89245365 | Out-Null
}

$Global:lastSendWaitTime = Get-Date

# ///////////////////////////////////
# //////////// FUNCTIONS ////////////
# ///////////////////////////////////

function start-trace {
    if ($Trace) {
        $fullpath = $CurrentPath + "\productivity_trace.wprp"
        wpr.exe -start $fullpath -filemode
    }
}

function stop-trace {
    param( [string]$file )
    if ($Trace) {
    	$fullpath = $Tracepath + "\" + $file
    	Write-Host "Saving: $fullpath"
        wpr.exe -stop $fullpath
    }
}

function mark-trace {
    param( [string]$comment )
    if ($Trace) {
        wpr.exe -marker "$comment"
    }
}

function Start-SleepFactor {
    param(
    [Parameter(ParameterSetName="m", Position=0, Mandatory=$true)]
    [int]$m,
    [Parameter(ParameterSetName="milliseconds", Position=0, Mandatory=$true)]
    [int]$milliseconds,
    [Parameter(ParameterSetName="s", Position=0, Mandatory=$true)]
    [int]$s,
    [Parameter(ParameterSetName="seconds", Position=0, Mandatory=$true)]
    [int]$seconds
    )

    switch($PsCmdlet.ParameterSetName)
    {
        {"milliseconds"} {Start-Sleep -m ($SleepFactor*$milliseconds)}
        {"seconds"} {Start-Sleep -m ($SleepFactor*$seconds*1000)}
        {"m"} {Start-Sleep -m ($SleepFactor*$m)}
        {"s"} {Start-Sleep -m ($SleepFactor*$s*1000)}
    }
}

#Function requires browser to be open already and in focus, navigates to the page given
function go-to-webpage{
	param( [string]$website )
#	$website = $website.ToCharArray
	Start-SleepFactor -m 500
    SendWait("^l")
	Start-SleepFactor -m 500
	type-string -stringToType $website
	Start-SleepFactor -m 500
	SendWait("{ENTER}")
    if($Debug){$Global:Keycount+=2}
}

#Types out the given string at normal human speed
function type-string{
	param( [string]$stringToType )
	$stringArray = $stringToType.ToCharArray()
	Start-SleepFactor -m 1000
	For ($ii = 0; $ii -lt $stringArray.Count; $ii++){
        switch($stringArray[$ii]){
                # Handles all special characters
            "(" {SendWait("{(}")}
            ")" {SendWait("{)}")}
            "+" {SendWait("{+}")}
            "^" {SendWait("{^}")}
            "%" {SendWait("{%}")}
            "~" {SendWait("{~}")}
            default {SendWait($stringArray[$ii])}
        }

		# This delay determines The time between keystrokes (wpm)
        Start-SleepFactor -m 140
        
		if($stringArray[$ii] -eq "." -and $stringArray[$ii + 1] -eq " "){
            Start-SleepFactor -m 2000
        }
	}
    if($Debug){$Global:Keycount+=$stringArray.Count}
}

#Forward the given email to the address specified with the given message Body
function Forward_Email{
	param( [string]$PathToEmail,
		[string]$AddressToSendTo,
		[string]$Body
	)
	
	start $PathToEmail
	Start-SleepFactor -s 5
	
	SendWait("^f")
	Start-SleepFactor -s 2
	
	type-string($AddressToSendTo)
	Start-SleepFactor -s 1
	SendWait("{TAB}{TAB}")
	
	Start-SleepFactor -s 1
	type-string($Body)
	SendWait("{ENTER}")
        type-string((get-Date))

	Start-SleepFactor -s 3
	SendWait("%s")
	
    if($Debug){
        $Global:Keycount+=4
        Write-Host Forwarding Message:`t`t`t$PathToEmail
        Write-Host To Address:`t`t`t$AddressToSendTo
    }
}

#Replies to the open email with the given message body
function Reply_Email{
	param(  [string]$Body,
            [string]$AddressToReply
	)
	
	SendWait("^+r")
	Start-SleepFactor -s 1
    if($AddressToReply -ne $NULL){
        SendWait("+{TAB}")
        SendWait("+{TAB}")
        SendWait("+{TAB}")
        Start-SleepFactor -m 500
        SendWait("^a")
        Start-SleepFactor -m 500
        mark-trace "Outlook-Type Address"
        type-string($AddressToReply)
        Start-SleepFactor -s 1
        SendWait("{TAB}")
        SendWait("{TAB}")
        SendWait("{TAB}")
    }
    Start-SleepFactor -s 1
    mark-trace "Outlook-Type body"
	type-string($Body)
    SendWait("{ENTER}")
    type-string((get-Date))
	
	Start-SleepFactor -s 2
    mark-trace "Outlook-Send email"
	SendWait("%s")
	
    if($Debug){
        $Global:Keycount+=9
        Write-Host Replying To Email At:`t`t`t$AddressToReply
    }
}

#Send a new email to the address specified with the given message Body, HAVE OUTLOOK OPEN AND IN FOCUS
function New_Email{
	param([string]$AddressToSendTo,
		[string]$Body
	)
    mark-trace "Outlook-Compose new email"
	SendWait("^n")
	Start-SleepFactor -s 4

    mark-trace "Outlook-Type address"	
	type-string($AddressToSendTo)
	Start-SleepFactor -s 2
	SendWait("{TAB}{TAB}")
	Start-SleepFactor -s 2

    mark-trace "Outlook-Type subject"   
    type-string("Meeting Notes: " + (Get-Date))
    Start-SleepFactor -s 2
    
	SendWait("{TAB}")
	Start-SleepFactor -s 2

    mark-trace "Outlook-Type body"   
	type-string($Body)
    SendWait("{ENTER}")
    type-string((get-Date))
	
	Start-SleepFactor -s 2
    mark-trace "Outlook-Send email"
	SendWait("%s")
	
    if($Debug){
        $Global:Keycount+=5
        Write-Host Sending New Mail To:`t`t`t$AddressToSendTo
    }
}

#Send a new email to the address specified with the given message Body, HAVE OUTLOOK OPEN AND IN FOCUS
function HTML_Email{
	param([string]$PathToFiles,
        [string]$AddressToSendTo
	)
    mark-trace "Outlook-Open html message"   
    $msg = "html_" + ((Get-Random -maximum 101 -minimum 0) % 6) + ".msg"
	start ($PathToFiles + $msg)
	Start-SleepFactor -s 2
	
    mark-trace "Outlook-Forward message"   
	SendWait("^f")
    
	Start-SleepFactor -s 2
    mark-trace "Outlook-Type address"   
	type-string($AddressToSendTo)
	Start-SleepFactor -s 2
	SendWait("{TAB}{TAB}")
	Start-SleepFactor -s 2
	SendWait("^a")
	Start-SleepFactor -m 300
    mark-trace "Outlook-Type subject"   
    type-string("Promotional Material for Review: " + (Get-Date))
    Start-SleepFactor -s 2
    
	SendWait("{TAB}")

	Start-SleepFactor -s 2
    mark-trace "Outlook-Send email"
	SendWait("%s")
    Start-SleepFactor -s 2
	SendWait("{ESC}")

	if($Debug){
        $Global:Keycount+=7
        Write-Host Sending HTML Message:`t`t`t$msg
        Write-Host To Address:`t`t`t`t$AddressToSendTo
    }
}

# splits an array into defined elements or a defined element size
function Split-array {

<#  
  .SYNOPSIS   
    Split an array 
  .PARAMETER inArray
   A one dimensional array you want to split
  .EXAMPLE  
   Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -parts 3
  .EXAMPLE  
   Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -size 3
#> 

  param($inArray,[int]$parts,[int]$size)
  
  if ($parts) {
    $PartSize = [Math]::Ceiling($inArray.count / $parts)
  } 
  if ($size) {
    $PartSize = $size
    $parts = [Math]::Ceiling($inArray.count / $size)
  }

  $outArray = @()
  for ($i=1; $i -le $parts; $i++) {
    $start = (($i-1)*$PartSize)
    $end = (($i)*$PartSize) - 1
    if ($end -ge $inArray.count) {$end = $inArray.count}
    $outArray+=,@($inArray[$start..$end])
  }
  return ,$outArray

}

# Generate random data in a two columns, with options for number of data points, data graphing, and random use of formulas 
function Generate_Random_Excel{
	param(  [Bool]$Graph,
            [int]$Size,
            [Bool]$Formulas
	)

    Start-SleepFactor -m 100
	
	SendWait("^{HOME}")
	Start-SleepFactor -m 50
	$a = 0
    $b = 0	

	DO{
	    $x = Get-Random -maximum 100 -minimum 0
	    Start-SleepFactor -m 50
	    $y = Get-Random -maximum 1000 -minimum 0
        
        Start-SleepFactor -m 50
        mark-trace "Excel-Type number"
	    type-string -stringToType $x
	    Start-SleepFactor -m 50
        mark-trace "Excel-Move to next cell right"
	    SendWait("{RIGHT}")
	    Start-SleepFactor -m 100
        # Insert averaging at random points after run 10, 1/10 chance of formula if enabled
        If ($Formulas -eq $True -AND $a -gt 10 -AND ($y % 10) -eq 0){
            # Average the previous 10 cells
            mark-trace "Excel-Type formula"
            type-string -stringToType "=AVERAGE(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-10,0,10,1))"
        }else{
            mark-trace "Excel-Type number"
            type-string -stringToType $y
	        Start-SleepFactor -m 100
        }
        mark-trace "Excel-Type ENTER and LEFT"
	    SendWait("{ENTER}")
	    Start-SleepFactor -m 100
	    SendWait("{LEFT}")
	    Start-SleepFactor -m 50
	    $a++
        if($Debug){$Global:Keycount+=3}
	}While ($a -le $Size)
    
    #If option is set, graph the new data after generating
    If ($Graph -eq $True){
        mark-trace "Excel-Command to graph"
        SendWait("{RIGHT}{UP}")
        Start-SleepFactor -m 100
	    
        DO{
            
            SendWait("+{UP}")
	        Start-SleepFactor -m 100
            $b++
            if($Debug){$Global:Keycount++}
        }while ($b -le $Size)
        SendWait("+{LEFT}")
	    Start-SleepFactor -m 50
	    SendWait("%")
	    Start-SleepFactor -m 50
	    SendWait("n")
	    Start-SleepFactor -m 50
	    SendWait("d")
	    Start-SleepFactor -m 50
	    SendWait("{RIGHT}")
	    Start-SleepFactor -m 200
        SendWait("{ENTER}")
        mark-trace "Excel-Graph"
	    Start-SleepFactor -m 50
        if($Debug){$Global:Keycount+=8}
    }
    
}

#Get random Body Text for an email. Uses textfiles named Email_#.txt where # is a number from 0 - 4
function getRandomEmailText{
    param( [string]$PathToFiles)
    
    $i = ((Get-Random -maximum 101 -minimum 0) % 4)

    $body = (Get-Content ($PathToFiles + "Email_" + $i + ".txt"))

    if($Debug){
        Write-Host Random Email Selected:`t`t`t($PathToFiles + "Email_" + $i + ".txt")
    }

    $body
    Return
}

#Sends three page down commands over 15 seconds
function slowPageScroll{
    Start-SleepFactor -s 5
    SendWait("{DOWN}{PGDN}")
    Start-SleepFactor -s 3
    SendWait("{PGDN}")
    Start-SleepFactor -s 4
    SendWait("{PGDN}")
    Start-SleepFactor -s 3
    if($Debug){$Global:Keycount+=4}
}

#Initialization for the switchFocus function
function initFocus{
# add a C# class to access the WIN32 API SetForegroundWindow
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class StartActivateProgramClass {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@
}

#Changes what application is the primary focus
function switchFocus{
    param(  [string]$ApplicationTitle,
            [string]$ProcessName)

    if ($ProcessName -ne "") {
        $p = Get-Process | Where-Object { $_.ProcessName -eq $ProcessName }
    }
    else {
        $p = Get-Process | Where-Object { $_.MainWindowTitle.ToLower().Contains($ApplicationTitle.ToLower()) -and !$_.MainWindowTitle.ToLower().Contains("command") -and !$_.MainWindowTitle.ToLower().Contains("powershell") }
    }
    if ($Debug) {$p}
    if ($p -ne $null) {
        $h = $p[0].MainWindowHandle
        # set the application to foreground
        [void] [StartActivateProgramClass]::SetForegroundWindow($h)
    }

}

#Write in OneNote, note:new tabs or pages will cause permanent filesize increase
function Write_To_OneNote{
    param(  [string]$TextToWrite,
            [string]$Title,
            [switch]$NewTab,
            [switch]$NewPage,
            [switch]$placeTitle = $false)
    if ($NewTab){
        $placeTitle = $True
        SendWait("^t")
        Start-SleepFactor -m 400
        SendWait("{ENTER}")
        Start-SleepFactor -m 800
        if($Debug){$Global:Keycount+=2}
    }
    if ($NewPage){
        $placeTitle = $True
        SendWait("^n")
        Start-SleepFactor -m 800
        if($Debug){$Global:Keycount++}
    }
    if ($placeTitle){
        SendWait("^+t")
        type-string -stringToType $Title
        Start-SleepFactor -m 800
        SendWait("{ENTER}")
        Start-SleepFactor -m 800
        if($Debug){$Global:Keycount++}
    }

    SendWait("^a")
    mark-trace "OneNote-Type"
    type-string -stringToType $TextToWrite
    mark-trace "OneNote-Sleep 5s to let sync"
    Start-SleepFactor -s 5
    if($Debug){$Global:Keycount++}
}

#Funcion Takes a screen shot of the specified screen area and save to the specified file (Used in Debug)
function screenshot(){
    param(  [Drawing.Rectangle]$bounds, 
            [string]$path)
   $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
   $graphics = [Drawing.Graphics]::FromImage($bmp)

   $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)

   $bmp.Save($path)

   $graphics.Dispose()
   $bmp.Dispose()
}

#Send keys and wait for the keystroke messages to be process + cancel event checking.
function SendWait
{
    Param(
        [parameter(Position=0, Mandatory=$true)][string]$keys
    )

    [System.Windows.Forms.SendKeys]::SendWait($keys)

    $cancel = $false
    $currentTime = Get-Date
    $duration = New-TimeSpan -Start $Global:lastSendWaitTime -End $currentTime
    if ($duration.TotalSeconds -ge 5) {
        $cancel = $Global:axeSupport.CancelEvent.WaitOne(0)
        $Global:lastSendWaitTime = $currentTime
    }

    if ($cancel) {
        throw "Cancel Event Fired"
    }
}

#Close all applications we invoked
function CloseAllApps
{
    $allApps = @("OUTLOOK", "EXCEL", "POWERPNT", "WINWORD")

    foreach ($app in $allApps) {
        $p = Get-Process | Where-Object { $_.ProcessName -eq $app }
        if ($p -ne $null) {
            Stop-Process -ProcessName $app -ErrorAction SilentlyContinue
        }
    }

    $app = "ONENOTE"

    Stop-Process -ProcessName $app -ErrorAction SilentlyContinue
    Stop-Process -ProcessName ONENOTEM -ErrorAction SilentlyContinue
    Stop-Process -ProcessName ONENOTE -ErrorAction SilentlyContinue
    Stop-Process -ProcessName ONENOTEIM -ErrorAction SilentlyContinue
}

#  ///////////////////////////////////////////////
#  //////////////////// MAIN /////////////////////
#  ///////////////////////////////////////////////
if ($screens -or $Debug){
    if(!(Test-Path((Convert-Path .) + "\Debug\"))){
        md ((Convert-Path .) + "\Debug\")
    }
}

if($screens){
    $Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
    $bounds = [Drawing.Rectangle]::FromLTRB($Screen.Left, $Screen.Top, $Screen.Width*2, $Screen.Height*2)
}

if ($Debug){
    $sw_outlook = [Diagnostics.Stopwatch]::StartNew()
    $sw_excel = [Diagnostics.Stopwatch]::StartNew()
    $sw_word = [Diagnostics.Stopwatch]::StartNew()
    $sw_ppt = [Diagnostics.Stopwatch]::StartNew()
    $sw_oneNote = [Diagnostics.Stopwatch]::StartNew()
    $sw_oneNote.Stop()
    $sw_outlook.Stop()
    $sw_excel.Stop()
    $sw_word.Stop()
    $sw_ppt.Stop()
    Start-Transcript -Append -Force -Path ((Convert-Path .) + "\Debug\Log.txt") 

    write-host "Starting:"`t (get-date -format hh:mm:ss)
}

if ($Address[$Address.Length - 1] -ne ";"){
    $Address = $Address + ";"
}
if ($ReplyAddress[$ReplyAddress.Length - 1] -ne ";"){
    $ReplyAddress = $ReplyAddress + ";"
}

$sw.Start()
initFocus

try {
    Import-Module (Join-Path -Path "$PSScriptRoot\Scripts" -ChildPath 'ProductivityPSModule.psm1') -ErrorAction Stop
    $procArch = Get-ProcessorArchitecture
    $ResourcePath = Join-Path -path $PSScriptRoot resources
    $ScriptsPath = Join-Path -path $PSScriptRoot scripts

    #*****************************************************************
    #
    #               AXE setup
    #
    #*****************************************************************

    # AXE termination signal handling loop
    $Global:TerminationLoop = $true

    # AXE environment and framework
	$AXEResultsPath = (Get-Item env:\AssessmentResultsPath).value
    $AXETempPath = (Get-Item env:\AssessmentTempPath).value
    $AxeBinPath = (Get-Item env:\AssessmentAxeBinPath).value
    $AxeCoreNet = "$AxeBinPath\Microsoft.Assessments.Core.dll"

    # workaround - we're not allowed to directly write results as an EE workload (current AXE limitation)
    # write to top level job results folder
    # make global since it needs to be referenced by timer actions
    #
    $Global:WorkloadResultsPath = [string] (Resolve-Path -Path "$AXEResultsPath\..\..\..\..\000_EnergyEfficiency\results")

    $LogFile = "$Global:WorkloadResultsPath\log.txt"
    # Print the values to log.txt
    "AXEResultsPath: $AXEResultsPath" | Out-File $LogFile -Force -Append
    "AXETempPath: $AXETempPath" | Out-File $LogFile -Force -Append
    "AxeBinPath: $AxeBinPath" | Out-File $LogFile -Force -Append
    
    # load up AXE framework from AXE DLL
    # initialize Runtime and create Logger helper object
    #
	"Load AXE..." | Out-File $LogFile -Force -Append
    $boolRunningUnderAxe = InitializeAxeLogger($AxeCoreNet)

    "AXE loaded succesfully..." | Log-Info
	
	while (([convert]::ToUInt64($sw.Elapsed.TotalSeconds)) -lt $RunTime) {
	    # ///////////////////////////////////////////////////////////////
	    # /////// SECTION 1: Open an email and send a reply to it ///////
	    # ///////////////////////////////////////////////////////////////
		"Outlook open and reply to email- START" | Log-Info
	    start-trace

	    if ($Debug){
	        write-host "Section 1:"`t (get-date -format hh:mm:ss)
	        $sw_outlook.Start()
	    }
	    $OutlookPath = ($FilesPath + $EmailFilename)
	    #$appOutlook = start-process -Filepath $OutlookPath

	    mark-trace "Outlook-Launch app"
	    start-process Outlook.exe
	    Start-SleepFactor -s 12
        mark-trace "Outlook-Focus"
	    #switchFocus -ApplicationTitle "Outlook"
        switchFocus -ProcessName ("OUTLOOK")
	    Start-SleepFactor -s 1

	    SendWait("%{Tab}")
		Start-SleepFactor -Milliseconds 500
	    SendWait("%{Tab}")

        mark-trace "Outlook-Maximize"
	    &$scripts\MaximizeWindow.ps1 -ApplicationTitle "Outlook"
	    Start-SleepFactor -s 1

	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5

        mark-trace "Outlook-Get body text from disk"
	    $EmailTxt = getRandomEmailText -PathToFiles $FilesPath

        mark-trace "Outlook-Reply to email"
	    Reply_Email -Body $EmailTxt -AddressToReply $ReplyAddress
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 1") + ".png")}
	    Start-SleepFactor -s 5
	    SendWait("{ESC}")
	    Start-SleepFactor -s 3
	    if ($Debug){
	        $sw_outlook.Stop()
	        $Global:Keycount+=2
	    }

	    stop-trace("trace_Outlook1.etl")
		"Outlook open and reply to email- END" | Log-Info

	    # /////////////////////////////////////////////////
	    # /////// SECTION 2: Start working in Word ////////
	    # /////////////////////////////////////////////////
		"Working with Word - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 2:"`t (get-date -format hh:mm:ss)
	        $sw_word.Start()
	    }
        mark-trace "Word-Get content from disk"
	    $txt = Get-Content ($FilesPath + $TextFilename)

        mark-trace "Copy Word File to Documents"
        $DocPath = $FilesPath + $DocFileName
        $LocalDir = $ENV:USERPROFILE + "\documents\"
        $LocalPath = $LocalDir + $DocFileName
        Copy-Item $DocPath $LocalPath -Force

        # Add this folder to trusted location so that Word won't open this file in protected view.
        REG ADD "HKCU\Software\Microsoft\Office\16.0\Word\Security\Trusted Locations\Location0" /v Path /d "$LocalDir" /t REG_EXPAND_SZ /f

        mark-trace "Word-Launch app"
	    Start-Process $LocalPath
	    #Start-Process $DocPath

	    Start-SleepFactor -s 3

        mark-trace "Word-Maximize window"
	    &$scripts\MaximizeWindow.ps1 -ApplicationTitle $DocFileName


	    Start-SleepFactor -s 5

        mark-trace "Word-Focus"
	    #switchFocus -ApplicationTitle "Word"
        switchFocus -ProcessName ("WINWORD")
	    Start-SleepFactor -m 50
	    SendWait("^a")
	 
	    #divide the txt file into parts and type out first part
	    $txt = $txt -split " "
	    $typeMe = Split-array -inArray @($txt) -size 22
	    $textArr = 0
        mark-trace "Word-Type part 1"
	    type-string -stringToType $typeMe[$textArr++]
	    #$textArr++

	    Start-SleepFactor -s 5
        mark-trace "Word-Type part 2"
	    type-string -stringToType $typeMe[$textArr++]
	    #type-string -stringToType $typeMe[$textArr++]
	    Start-SleepFactor -m 800
        mark-trace "Word-Save"
	    SendWait("^s")
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 2") + ".png")}
	    Start-SleepFactor -s 2
	    if ($Debug){
	        $sw_word.Stop()
	        $Global:Keycount+=2
	    }
	    stop-trace("trace_Word2.etl")
		"Working with Word - END" | Log-Info

	    # //////////////////////////////////////////////
	    # //////////// SECTION 3: New Email ////////////
	    # //////////////////////////////////////////////
		"Outlook new email - START" | Log-Info
	    start-trace
        mark-trace "Outlook-Focus"
	    #switchFocus -ApplicationTitle "Outlook"
        switchFocus -ProcessName ("OUTLOOK")
	    if ($Debug){
	        write-host "Section 3:"`t (get-date -format hh:mm:ss)
	        $sw_outlook.Start()
	    }
        mark-trace "Outlook-Get body text from disk"
	    $EmailTxt = getRandomEmailText -PathToFiles $FilesPath
        mark-trace "Outlook-New email"
	    new_Email -AddressToSendTo $Address -Body $EmailTxt
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 3") + ".png")}
	    Start-SleepFactor -s 5
	    if ($Debug){
	        $sw_outlook.Stop()
	    }
	    stop-trace("trace_Outlook3.etl")
		"Outlook new email - END" | Log-Info

	    # ////////////////////////////////////////////////////
	    # /////// SECTION 4: Continue writing in Word ////////
	    # ////////////////////////////////////////////////////
		"Continue writing in Word - START" | Log-Info 		
	    start-trace
	    if ($Debug){
	        write-host "Section 4:"`t (get-date -format hh:mm:ss)
	        $sw_word.Start()
	    }
        mark-trace "Word-Focus"
	    #switchFocus -ApplicationTitle "Word"
        switchFocus -ProcessName ("WINWORD")
	    Start-SleepFactor -m 50

        mark-trace "Word-Type part 3"
	    type-string -stringToType $typeMe[$textArr++]

	    Start-SleepFactor -s 3
        mark-trace "Word-Type part 4"
	    type-string -stringToType $typeMe[$textArr++]
	    #Start-SleepFactor -s 10
	    #type-string -stringToType $typeMe[$textArr++]
	    Start-SleepFactor -m 800
        mark-trace "Word-Save"
	    SendWait("^s")
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 4") + ".png")}
	    Start-SleepFactor -s 2
	    if ($Debug){
	        $sw_word.Stop()
	        $Global:Keycount++
	    }
	    stop-trace("trace_Word4.etl")
		"Continue writing in Word - END" | Log-Info

	    # /////////////////////////////////////////////
	    # //////// SECTION 5: Send HTML email /////////
	    # /////////////////////////////////////////////
		"Send HTML email - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 5:"`t (get-date -format hh:mm:ss)
	        $sw_outlook.Start()
	    }
        mark-trace "Outlook-Focus"
	    #switchFocus -ApplicationTitle "Outlook"
        switchFocus -ProcessName ("OUTLOOK")
        mark-trace "Outlook-Start HTML Email"
	    HTML_Email -PathToFiles $FilesPath -AddressToSendTo $Address
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 5") + ".png")}
	    Start-SleepFactor -s 15
        mark-trace "Outlook-Focus"
	    switchFocus -ApplicationTitle "Outlook"
	    Start-SleepFactor -m 50
        mark-trace "Outlook-Close app"
	    SendWait("%{F4}")
	    start-SleepFactor -s 3
	    if ($Debug){
	        $sw_outlook.Stop()
	        $Global:Keycount++
	    }
	    stop-trace("trace_Outlook5.etl")
		"Send HTML email - END" | Log-Info

	    # /////////////////////////////////////////////
	    # ////////// SECTION 6: Start Excel ///////////
	    # /////////////////////////////////////////////
		"Working with Excel - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 6:"`t (get-date -format hh:mm:ss)
	        $sw_excel.Start()
	    }
	    $ExcelPath = $FilesPath + $ExcelFileName
        mark-trace "Excel-Launch app"
	    Start-Process -FilePath $ExcelPath

	    Start-SleepFactor -s 3
        mark-trace "Excel-Focus"
	    #switchFocus -ApplicationTitle "Excel"
        switchFocus -ProcessName ("EXCEL")

        mark-trace "Excel-Maximize"
	    &$scripts\MaximizeWindow.ps1 -ApplicationTitle $ExcelFileName

	    Start-SleepFactor -s 3

        mark-trace "Excel-Generate table"
	    Generate_Random_Excel -Size 43 -Graph $True -Formulas $True
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 6") + ".png")}
	    Start-SleepFactor -s 5
	    mark-trace "Excel-Copy"
        SendWait("^c")
	    if ($Debug){
	        $sw_excel.Stop()
	        $Global:Keycount++
	    }
	    stop-trace("trace_Excel6.etl")
		"Working with Excel - END" | Log-Info

	    # /////////////////////////////////////////////
	    # //////// SECTION 7: Start PowerPoint ////////
	    # /////////////////////////////////////////////
	    "Working with PowerPoint - START" | Log-Info
		start-trace
	    if ($Debug){
	        write-host "Section 7:"`t (get-date -format hh:mm:ss)
	        $sw_ppt.Start()
	    }
	    $PPTPath = $FilesPath + $PPTFileName
        mark-trace "PowerPoint-Launch app"
	    Start-Process $PPTPath

	    Start-SleepFactor -s 3
        mark-trace "PowerPoint-Focus"
	    #switchFocus -ApplicationTitle ("PowerPoint")
        switchFocus -ProcessName ("POWERPNT")
        mark-trace "PowerPoint-Maximize"
	    &$scripts\MaximizeWindow.ps1 -ApplicationTitle $PPTFileName

	    Start-SleepFactor -s 5

        mark-trace "PowerPoint-Scroll pages"
	    slowPageScroll
	    slowPageScroll
	    slowPageScroll
	    slowPageScroll
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 7") + ".png")}
	    Start-SleepFactor -m 2500
        mark-trace "PowerPoint-Paste Excel table into new slide"
	    SendWait("^m")
	    Start-SleepFactor -m 800
	    SendWait("^a")
	    Start-SleepFactor -m 500
	    SendWait("{DELETE}")
	    Start-SleepFactor -m 800

	    SendWait("^v")
	    Start-SleepFactor -s 5
	    if ($Debug){
	        $sw_ppt.Stop()
	        $Global:Keycount+=4
	    }
	    stop-trace("trace_PPT7.etl")
		"Working with PowerPoint - END" | Log-Info

	    # /////////////////////////////////////////////
	    # /////////// SECTION 8: Use OneNote //////////
	    # /////////////////////////////////////////////
		"Working with OneNote - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 8:"`t (get-date -format hh:mm:ss)
	        $sw_oneNote.Start()
	    }
        mark-trace "OneNote-Launch app"
        if ($OneNoteUWP)
        {
            Start-Process onenote:"$OneNotePageLink"
        }
        else
        {
	        Start-Process ONENOTE.exe
        }
	    Start-SleepFactor -s 5
        mark-trace "OneNote-Focus"
	    #switchFocus -ApplicationTitle "OneNote"
        switchFocus -ProcessName ("ONENOTE")
	    Start-SleepFactor -m 200
        mark-trace "OneNote-Maximize"
	    &$scripts\MaximizeWindow.ps1 -ApplicationTitle "OneNote"
	    Start-SleepFactor -s 2
	    Write_To_OneNote -TextToWrite $typeMe[0] -Title "Albert Einstein Biography" -placeTitle $True
	    Write_To_OneNote -TextToWrite $typeMe[1]
	    Write_To_OneNote -TextToWrite $typeMe[2]
	    Write_To_OneNote -TextToWrite $typeMe[3]
	    Write_To_OneNote -TextToWrite $typeMe[4]
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 8") + ".png")}
	    Start-SleepFactor -s 5
	    SendWait("%{F4}")
	    Start-SleepFactor -s 3
        mark-trace "OneNote-Kill process"
		Stop-Process -ProcessName ONENOTEM -ErrorAction SilentlyContinue
		Stop-Process -ProcessName ONENOTE -ErrorAction SilentlyContinue
		Stop-Process -ProcessName ONENOTEIM -ErrorAction SilentlyContinue

	    if ($Debug){
	        $sw_oneNote.Stop()
	        $Global:Keycount++
	    }
	    stop-trace("trace_OneNote8.etl")
		"Working with OneNote - END" | Log-Info

	    # /////////////////////////////////////////////
	    # ///////// SECTION 9: Close Excel ////////////
	    # /////////////////////////////////////////////
		"Closing Excel - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 9:"`t (get-date -format hh:mm:ss)
	        $sw_excel.Start()
	    }
        mark-trace "Excel-Focus"
	    #switchFocus -ApplicationTitle "Excel"
        switchFocus -ProcessName ("EXCEL")
	    Start-SleepFactor -s 2
        mark-trace "Excel-Quit without saving"
	    SendWait("%{F4}")
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 9") + ".png")}
	    Start-SleepFactor -m 100
	    SendWait("n")
	    Start-SleepFactor -m 100
	    if ($Debug){
	        $sw_excel.Stop()
	        $Global:Keycount+=2
	    }
	    stop-trace("trace_Excel9.etl")
		"Closing Excel - END" | Log-Info

	    # /////////////////////////////////////////////
	    # ///// SECTION 10: Start PowerPoint show /////
	    # /////////////////////////////////////////////
		"PowerPoint slide show - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 10:"`t (get-date -format hh:mm:ss)
	        $sw_ppt.Start()
	    }
        mark-trace "PowerPoint-Focus"
        #switchFocus -ApplicationTitle ("PowerPoint")
	    switchFocus -ProcessName ("POWERPNT")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Initiate slide show"
	    SendWait("{F5}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 2"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 3"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 4"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 5"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 6"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 7"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 8"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 9"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 10"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 11"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 12"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 13"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 14"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 15"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 16"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 17"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 18"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 19"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 10
        mark-trace "PowerPoint-Page 20"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 5
        mark-trace "PowerPoint-Page 21"
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 1
        mark-trace "PowerPoint-Page 22"
	    SendWait("{ESC}")
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 10") + ".png")}
	    Start-SleepFactor -s 7
	    if ($Debug){
	        $sw_ppt.Stop()
	        $Global:Keycount+=22
	    }
	    stop-trace("trace_PPT10.etl")
		"PowerPoint slide show - END" | Log-Info

	    # /////////////////////////////////////////////
	    # /////// SECTION 11: Close PowerPoint ////////
	    # /////////////////////////////////////////////
		"PowerPoint close - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 11:"`t (get-date -format hh:mm:ss)
	        $sw_ppt.Start()
	    }
        mark-trace "PowerPoint-Focus"
	    #switchFocus -ApplicationTitle ("PowerPoint")
        switchFocus -ProcessName ("POWERPNT")
        mark-trace "PowerPoint-Quit without saving"
	    SendWait("%{F4}")
	    Start-SleepFactor -m 100
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 11") + ".png")}
	    SendWait("n")
	    Start-SleepFactor -s 2
	    if ($Debug){
	        $sw_ppt.Stop()
	        $Global:Keycount+=2
	    }
	    stop-trace("trace_PPT11.etl")
		"PowerPoint close - END" | Log-Info

	    # /////////////////////////////////////////////
	    # /////// SECTION 12: Send a new email ////////
	    # /////////////////////////////////////////////
		"Send a new email - START" | Log-Info
	    start-trace
	    if($Debug){
            $sw_outlook.Start()
        write-host "Section 12:"`t (get-date -format hh:mm:ss)
        }
        mark-trace "Outlook-Launch app"
	    start-process Outlook.exe
	    Start-SleepFactor -s 12
        mark-trace "Outlook-Maximize"
	    &$scripts\MaximizeWindow.ps1 -ApplicationTitle "Outlook"
	    Start-SleepFactor -s 1
        mark-trace "Outlook-Focus"
	    #switchFocus -ApplicationTitle "outlook"
        switchFocus -ProcessName ("OUTLOOK")
	    $EmailTxt = getRandomEmailText -PathToFiles $FilesPath
        mark-trace "Outlook-New Email"
	    New_Email -Body $EmailTxt -AddressToSendTo $Address
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 12") + ".png")}
	    Start-SleepFactor -s 20
        mark-trace "Outlook-Close app"
	    SendWait("%{F4}")
	    Start-SleepFactor -s 2
	    SendWait("{ENTER}")
	    Start-SleepFactor -s 2
	    if($Debug){
	        $sw_outlook.Stop()
	        $Global:Keycount+=2
	    }
	    stop-trace("trace_Outlook12.etl")
	    "Send a new email - END" | Log-Info

	    # ///////////////////////////////////////////////
	    # ///// SECTION 13: Finish Writting in Word /////
	    # ///////////////////////////////////////////////
		"Finish writing in Word - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 13:"`t (get-date -format hh:mm:ss)
	        $sw_word.Start()
	    }
        mark-trace "Word-Focus"
	    #switchFocus -ApplicationTitle ("Word")
        switchFocus -ProcessName ("WINWORD")
	    Start-SleepFactor -m 50

        mark-trace "Word-Type part 5"
	    type-string -stringToType $typeMe[$textArr++]

	    Start-SleepFactor -s 10
        mark-trace "Word-Type part 6"
	    type-string -stringToType $typeMe[$textArr++]

	    Start-SleepFactor -m 800
        mark-trace "Word-Save"
	    SendWait("^s")
	    Start-SleepFactor -s 2
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 13") + ".png")}
	    #Start-SleepFactor -s 30
	    if ($Debug){
	        $sw_word.Stop()
	        $Global:Keycount++
	    }
	    stop-trace("trace_Word13.etl")
		"Finish writing in Word - END" | Log-Info

	    # ///////////////////////////////////////////////
	    # ///// SECTION 14: Close All Open Programs /////
	    # ///////////////////////////////////////////////
		"Closing all programs - START" | Log-Info
	    start-trace
	    if ($Debug){
	        write-host "Section 14:"`t (get-date -format hh:mm:ss)
	        $sw_word.Start()
	    }
        mark-trace "Word-Focus"
	    #switchFocus -ApplicationTitle "Word"
        switchFocus -ProcessName ("WINWORD")
	    Start-SleepFactor -m 50
        mark-trace "Word-Close app"
	    SendWait("%{F4}")
	    Start-SleepFactor -m 100

        mark-trace "Remove Word Document"
        Remove-Item $LocalPath -Force

	  #  SendWait("n")
	    Start-SleepFactor -s 2
	    if ($screens){screenshot -bounds $bounds -path ((Convert-Path .) + "\Debug\" + $((get-date -f MM.dd.yy-hh.mm)+ " - Section 14") + ".png")}
	    if ($Debug){
	        $sw_word.Stop()
	        $Global:Keycount++
	    }
	    stop-trace("trace_Close14.etl")
		"Closing all programs - END" | Log-Info
	}
} catch {
    #*****************************************************************
    #
    #                Handle exceptions
    #
    #*****************************************************************

    if ( $Global:axeLogger ) {
        $Global:axeLogger.LogErrorCode( $_.Exception.Hresult, $_ )

        # Write the exception details to a errors XML file
        $xmlErrorFile = "$AXEResultsPath\ErrorsAndWarnings.xml"
        $message = $_.Exception.Message + [Environment]::NewLine + $_.InvocationInfo.PositionMessage
        LogToXmlErrorFile -XmlErrorFile $xmlErrorFile -ErrorCode $_.Exception.Hresult -ErrorMessage $message
    }

    CloseAllApps

    throw
} finally {
	if ($Trace) {
		$status = wpr.exe -status
		if (!($status -contains "WPR is not recording")) {
			Write-Host "Cancelling WPR recording."
			wpr.exe -cancel | Out-Null
		}
	}

    # We should always properly dispose of this guy.
    #
    if ( $Global:axeSupport ) {
        $Global:axeSupport.Dispose()
    }
}

if ($Debug){
    $sw.Stop()
    write-host "Exit: " (get-date -format hh:mm:ss)`r`n
    write-host Total Time Ran: ([convert]::ToUInt64($sw.Elapsed.TotalSeconds)) `r`n
    write-host "Breakdown of Time"
    write-host "==================================="
    Write-Host Outlook:`t`t([convert]::ToUInt64($sw_outlook.Elapsed.TotalSeconds))`t`t([convert]::ToUInt64(([convert]::ToUInt64($sw_outlook.Elapsed.TotalSeconds)/[convert]::ToUInt64($sw.Elapsed.TotalSeconds))*100))%
    Write-Host Excel:`t`t`t([convert]::ToUInt64($sw_excel.Elapsed.TotalSeconds))`t`t([convert]::ToUInt64(([convert]::ToUInt64($sw_excel.Elapsed.TotalSeconds)/[convert]::ToUInt64($sw.Elapsed.TotalSeconds))*100))%
    Write-Host Word:`t`t`t([convert]::ToUInt64($sw_word.Elapsed.TotalSeconds))`t`t([convert]::ToUInt64(([convert]::ToUInt64($sw_word.Elapsed.TotalSeconds)/[convert]::ToUInt64($sw.Elapsed.TotalSeconds))*100))%
    Write-Host PowerPoint:`t`t([convert]::ToUInt64($sw_ppt.Elapsed.TotalSeconds))`t`t([convert]::ToUInt64(([convert]::ToUInt64($sw_ppt.Elapsed.TotalSeconds)/[convert]::ToUInt64($sw.Elapsed.TotalSeconds))*100))%
    Write-Host OneNote:`t`t([convert]::ToUInt64($sw_oneNote.Elapsed.TotalSeconds))`t`t([convert]::ToUInt64(([convert]::ToUInt64($sw_oneNote.Elapsed.TotalSeconds)/[convert]::ToUInt64($sw.Elapsed.TotalSeconds))*100))%
    Write-Host `r`nKey Count:`t`t$Global:Keycount
    Stop-Transcript
}
