Powershell Script for emailing PPCoin price on Vircurex

I have recently found myself checking the price of PPCoin on a daily basis. Obviously this sort of thing can be automated with a script, so here it is. Powershell was brought into service for this one: I haven’t got much experience with Powershell so had to google my way around, and learnt something in the process.

The Powershell Script

$url = "https://vircurex.com/api/get_info_for_currency.xml"
$xml = [xml](new-object system.net.webclient).downloadstring($url)
$from = "***@***.com"
$to = "***@***.com"
$subject = "PPC"
$server = "servername"
$smtp=new-object Net.Mail.SmtpClient($server)
$smtp.Send($from, $to, $subject,$xml.hash.PPC.BTC."highest-bid".get_InnerXml())

Obviously you’ll need to set the from and to addresses, and the servername correctly for your environment. Also, as I am using this on a domain joined PC, it uses the default credentials so I didn’t need to set any on the SmtpClient. I’m sure the above script could be improved so that you pass the to address in via an argument, but this is a quick and dirty hack so it doesn’t really matter. However, it definitely would benefit from some exception handling…

Scheduling the Script

I then saved this script and scheduled in Windows using the Task Scheduler. I had to use the full path of the 32-bit Powershell executable (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe on my Windows 7 box), and used -Command “D:\PPC.ps1”  for the arguments (where D:\PPC.ps1 is the full path of the script):

Powershell Scheduled Task

And there you go! Powershell to the rescue.

As a sidenote, I have had issues sending emails when using the SmtpClient in .NET and a domain-joined PC & user. If you’re getting a ‘Client does not have permissions to send as this sender‘ exception, then you need to ensure that the user ‘NT AUTHORITY\SELF’ has ‘Send-as’ permissions on the user in question. The following link helped me out with this issue: