I recently installed the open source iSpyConnect software for a surveillance camera and was put off to find that they wanted me to pay a subscription fee to get the version that emailed you when motion was detected. Kinda lame IMHO. I don’t mind paying for a “pro” version, but I really don’t like the subscription model for software (yes, I’m talking to you Adobe!).
Anyways, a quick Google search turned up this gem from a 2013 blog post. I don’t really know much about Windows PowerShell, but it’s similar to most scripting languages (DOS, VB, PERL, etc.), so it was fairly easy to do some minor tweaking to the original code to get it working on my system. After a few tests I got it going under Windows 8.1. It’s mostly the original code with a few modifications and updates. I have also found that for some unknown reason, this works more reliably if you call the Powershell script from a DOS batch file. There should be no difference, but this is my experience.
Here are the updated steps:
1. Open Notepad or your favorite text editor, enter the following Powershell code, and save it as c:\ispy\ispyalertmail.ps1
$date = get-date -format yyyy-MM-dd
$time = get-date -format H-mm-ss
$filename = $date+"_$time.jpg" #Define file name format. In my case, iSpy used: yyyy-MM-dd_H-mm-ss.jpg
$dir = "C:\Documents and Settings\Your_username\Application Data\iSpy\WebServerRoot\Media\video\FKJPK\grabs" #The filepath iSpy saves images to
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1 #Get the last created image
$filename = $latest.name
add-content c:\ispy\cam1log.txt "Motion detected at $date $time" #Create a log file
$file = $dir+"\$filename" #Define the email attachment file
$EmailFrom = "your_e-mail_address@whatever.com" #Your from e-mail address
$EmailTo = "destination_e-mail@whatever.com" #The e-mail the alert message will be sent to
$Subject = "Camera alert" #Define a subject
$Body = "Something moved at $time , $date. Check the live view. Recording done." #Define email text body
$SMTPServer = "smtp.gmail.com" #The SMTP server your host is using. Here we have used Gmails
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPMessage.Attachments.Add($file) #Attach the latest image
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) #The SMTP server port. Here we have used Gmails
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“your_e-mail@whatever.com”, “your super secure password”); #careful, your making your password visible to anyone who can access this script
$SMTPClient.Send($SMTPMessage) #Send the email
2. Create another text file and save it as c:\ispy\ispyalertmail.bat. Here’s the code:
powershell -ExecutionPolicy bypass -Command "c:\ispy\ispyalertmail.ps1"
3. Open the iSpy application. Right-click the camera image and choose Edit.
4. Click the Alerts tab
5. The When pop-up should already be set to Alert. That’s what we want.
6. On the Select Action pop-up, select Execute file and then click Add.
7. Click the … next to the File field and navigate to the batch file you created. It should be here c:\ispy\ispyalertmail.bat
8. Click OK.
9. Things should look like this:
10. Click the Finish button and you’re done.
11. When motion is detected and an alert is triggered, your Powershell script will be executed. You should see a DOS window flash open and close. If you get too many alerts, try adjusting the Movement seconds by clicking the … just to the left of where it says Plugins. I have mine set to 2 seconds, which means there has to be movement for two continuous seconds before a alert is triggered.
So far this is working swimmingly. Hope it works for you. Kudos to h311m4n for the original script. I couldn’t have done this without him.
If I goto Settings, SMTP and enter my credentials for my mail account, I’m able to add “Send email” under alerts, when edit the camera settings.
Hi,
this is a superb script. It works as expected.
I had to:
1. Confirm the directory iSpy uses, in my case was “C:\Users\PC_Name\AppData\Roaming\iSpy\WebServerRoot\Media\Video\CCIBX\grabs”
2. Turn on “Allow less secure apps” in your Google account (if using Gmail) if you get error 5.5.1 when executing the script.
Other than that, it works great!
Awesome David. Thanks for the additional info.
It throws an error with what looks like the username and password section:
PS C:\Windows\System32\WindowsPowerShell\v1.0> G:\Security\ispyimageemail.bat
C:\Windows\System32\WindowsPowerShell\v1.0>powershell -ExecutionPolicy bypass -Command “G:\Security\ispyimageemail.ps1”
At G:\Security\ispyimageemail.ps1:18 char:93
+ … ject System.Net.NetworkCredential (“removed@email.com”), (“removedpassword”) …
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : MissingArgument
I got the powershell script and batch file to work (sort of). it keeps giving me an error about not being able to attach the file (access denied). any ideas? It does send me an email now, but since it is getting that error it doesn’t have a file attached. I put my storage folder on a separate drive from where my user profile is saved, but it still gives the error even after I set the folder permissions so that users and everyone groups have full control of the files and folders at the new location.
My only suggestion is that you simplify everything and see if you can get it to work and just start re-building the complexity of the script. For example, put a photo named photo.jpg in a folder called test on your C: drive and use these variables:
$filename = “photo.jpg”
$dir = “C:\test”
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1 #Get the last created image
$filename = $latest.name
If that works, do a similar test set up on the drive that doesn’t work now. Use a folder right off the root. If it doesn’t work, then it probably is a permissions issue.
Sometimes you just gotta resort to trial and error :-/
–Doug
thanks for the suggestion. I know it’s probably something silly that I will kick myself for once I find the problem.
Isn’t that always the case! Good luck.
Thanks. Worked great. The script will bomb but still email if no image is in the directory when the alert is set off.
Did you add “powershell” before the “-ExecutionPolicy bypass -Command”?
Hi my friend ! Unfortunately i get this errors :
There were errors in loading the format data file:
Microsoft.PowerShell.Core. C:\WINDOWS\system32\WindowsPowershell\v1.0\PowerShellTrace.format.ps1xml: File not found.
Missing expression after unary operator ‘-‘
At line:1 char:2
+ -E <<<< xecutionPolicy bypass -Command C:\ispy\ispyalertmail.ps1
COULD YOU HELP ME what i do wrong ?
No idea. Sorry.