10 July, 2011

Powershell, Gmail, µTorrent (utorrent), PeerBlock

This post is further elaboration on this script:
http://rich-laduca.blogspot.com/2010/03/powershell-sending-ip-address-in-email.html


In addition to emailing me my IP. I wanted to also know if:

1) Is µTorrent running.
2) Is PeerBlock running
 If 1 was true,  and 2 was false... I wanted to know about it. And I also wanted it fixed ASAP!

This script will, if
µTorrent IS running,  start PeerBlock... And email the disposition.

Before this will work, you have to create a scheduled task to start PeerBolck.

Be sure to select “Run with Highest Privileges’ when setting up the task...
This is how the UAC is by-passed. otherwise PeerBlock cant be started from a
command line only. You have to acknowledge the UAC ~OR~ you can start the task:

schtasks /run /tn PeerGaurdian (I named the task ‘PeerGaurdian’ so that is what I am stating )

#############################################################################
#### This part is checking to see if utorrent and PeerBlock are running  ####
#### and will turn PeerBlock on if utorrent is running without PeerBlock ####
#############################################################################

#########################################################################
###                                                                   ###
### This script is designed to start PeerBlock if µTorrent is running ###
###                                                                   ###
#########################################################################

$ErrorActionPreference = “SilentlyContinue”
$Both =""
$utorrent = ""
$UTorName = ""
$peerblock = ""
$PeerBlockName = ""
$uTorr = ""
$PBlock = ""
$a = ""
$Username = ""
$PW = ""
$EmailFrom = ""
$EmailTo = ""
$Subject = ""
$Body =

$utorrent = Get-Process utorrent -ea 0
$UTorName = $utorrent.ProcessName.ToString()

$peerblock = Get-Process peerblock -ea 0
$PeerBlockName = $peerblock.ProcessName.ToString()

### if µTorrent is NOT running... Dont worry about it
### if µTorrent ISN'T running... Start PeerBlock
### This has to be done as a 'scheduled task' with elevated privleges in Win 7 to bypass UAC

## for some reason the 'ProcessName' was not working the way I wanted it to

If ($UTorName -eq "")
{
Get-Process utorrent
Get-Process peerblock
$uTorr = " µTorrent is NOT running"
}

## If Utorrent is running and PeerBlock aint... Start PeerBlock.
## The constant 'Get-Process' you see below was from me trying to
## figure out why 'ProcessName' was not working. I wanted to use
## 'Null' as the standard, but that just wasn't working

If ($UTorName -eq "utorrent" -And $PeerBlockName -ne "peerblock")
{
Get-Process utorrent
Get-Process peerblock
$Both = " *** µTorrent IS running. Starting PeerBlock..."
schtasks /run /tn PeerGaurdian
# Give PeerGuardian a chance to start
Start-Sleep -s 5
$peerblock = Get-Process peerblock -ea 0
$PeerBlockName = $peerblock.ProcessName.ToString()
Get-Process utorrent
Get-Process peerblock
}

If ($UTorName -ne "")
{
Get-Process utorrent
Get-Process peerblock
$uTorr = " µTorrent IS running."
}


If ($PeerBlockName -eq "")
{
Get-Process utorrent
Get-Process peerblock
$PBlock = " *** PeerBlock is NOT running"
}
Else
{
Get-Process utorrent
Get-Process peerblock
$PBlock = " PeerBlock IS running"
}


$a = Get-Date
$a = $a.ToShortTimeString()
###########################################################################
#### Now on to sending this info out along with the IP of the computer ####
###########################################################################

$wc=New-Object net.webclient
$Results = $wc.downloadstring("http://checkip.dyndns.com:8245/") -replace "[^\d\.]"
$http = "http://"
$8080 = ":8080/index.asp"

#############################################################################################
####                       NOTES about " -replace "[^\d\.]"                              ####
####                                                                                     ####
#### "-Replace" replace "^" all Characters excluding "\d" numbers or "\." decimal points ####
####  ~ it literally only retains numbers and decimal points ~                           ####
####                                                                                     ####
#############################################################################################

## GMail username and PW
$Username = "XXXXXXXX"
$PW = "****************"

$EmailFrom = "XXXXXXXX@gmail.com"
$EmailTo = "XXXXXXXX@gmail.com"
$Subject = "$Results - Home IP Address"

$Body = "$Results - Home IP Address
$a
$Both
$uTorr
$PBlock
ROUTER: $http$Results$8080

"

$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $PW);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

$Both =""
$utorrent = ""
$UTorName = ""
$peerblock = ""
$PeerBlockName = ""
$uTorr = ""
$PBlock = ""
$a = ""
$Username = ""
$PW = ""
$EmailFrom = ""
$EmailTo = ""
$Subject = ""
$Body = ""

No comments:

Post a Comment