Project Objective:
Monitor Star Wars The Old Republic (SWTOR) client for network problems or disconnects.

Problem to be solved:
At times I am disconnected from the SWTOR servers while flying around and or crafting but still running locally (or sometimes all together). I wrote this script to check and see when I was connected to the server properly and when I wasn’t. I then have the script send me an SMS message when I am not connected. I use SMS instead of normal mail because I am on my computer.

Please note that you will need to edit variables like your mail server and SMS email address, etc.

# # Watch-SWTOR-Client.ps1 # # Written by Aaron Wurthmann (aaron (AT) wurthmann (DOT) com) # # If you edit please keep my name as an original author and # keep me apprised of the changes, see email address above. # This code may not be used for commercial purposes. # You the executor, runner, user accept all liability. # This code comes with ABSOLUTELY NO WARRANTY. # You may redistribute copies of the code under the terms of the GPL v2. # ----------------------------------------------------------------------- # 2012.03.26 ver 2 # # Summary: # Checks number of connections swtor.exe has established. # Sends an email using gmail in the event that the client has been disconnected. # # ----------------------------------------------------------------------- # General Usage: # Edit the varibles below to specify your mial server, username, password and # where you want to send the mail to. # Start Star Wars The Old Republic, start the script from the PowerShell console. # function Get-NetworkStatistics { # From Shay Levy, PowerShell MVP $properties = 'Protocol','LocalAddress','LocalPort' $properties += 'RemoteAddress','RemotePort','State','ProcessName','PID' netstat -ano | Select-String -Pattern '\s+(TCP|UDP)' | ForEach-Object { $item = $_.line.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries) if($item[1] -notmatch '^\[::') { if (($la = $item[1] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6') { $localAddress = $la.IPAddressToString $localPort = $item[1].split('\]:')[-1] } else { $localAddress = $item[1].split(':')[0] $localPort = $item[1].split(':')[-1] } if (($ra = $item[2] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6') { $remoteAddress = $ra.IPAddressToString $remotePort = $item[2].split('\]:')[-1] } else { $remoteAddress = $item[2].split(':')[0] $remotePort = $item[2].split(':')[-1] } New-Object PSObject -Property @{ PID = $item[-1] ProcessName = (Get-Process -Id $item[-1] -ErrorAction SilentlyContinue).Name Protocol = $item[0] LocalAddress = $localAddress LocalPort = $localPort RemoteAddress =$remoteAddress RemotePort = $remotePort State = if($item[0] -eq 'tcp') {$item[3]} else {$null} } | Select-Object -Property $properties } } } do { Start-Sleep -s 60 $count=0 $date=Get-Date $swtorNetStat=Get-NetworkStatistics | Where-Object {$_.ProcessName -eq 'swtor'} | Where-Object {$_.State -eq 'ESTABLISHED'} | Format-Table $count=$swtorNetStat.count #$swtorNetStat if ($count -ge 6) {write-host "Status at" $date" : PASSED"} } while ($count -ge 6) write-host "Status at" $date" : FAILED" $EmailUser="USERNAME" $EmailPassword="PASSWORD" $EmailFrom="USERNAME@gmail.com" $Arry_EmailTo="##########@txt.att.net"

$SMTPServer = "smtp.gmail.com"

$SMTPPort=587

[string]$computername=$env:computername $EmailSubj='SWTOR is not connected on ' + $computername $EmailBody='SWTOR is not connected on ' + $computername + ' at ' + $date # From Christian @ StackOverflow.com $SMTPClient = New-Object Net.Mail.SMTPClient( $SmtpServer, $SmtpPort ) $SMTPClient.EnableSSL = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $EmailUser, $EmailPassword ); # From Core @ StackOverflow.com $emailMessage = New-Object System.Net.Mail.MailMessage $emailMessage.From = $EmailFrom foreach ( $recipient in $Arry_EmailTo ) { $emailMessage.To.Add( $recipient ) } $emailMessage.Subject = $EmailSubj $emailMessage.Body = $EmailBody $SMTPClient.Send( $emailMessage )