PushBullet + PowerShell = PowerBullet!

A friend and colleague of mine, Chris Townsend were talking over lunch recently, when he lamented that during a very long scripted process, he would have to manually check back in for status checks to see how this process was going, and might waste a whole weekend in front of the keyboard.  We began trying to think of ways to get notifications if he needed to intervene and manually do something.

We talked about e-mails, but I think it wouldn’t work because of no open SMTP relay.  Then we wondered about sending a text, and I was reminded of PushBullet.  If you’ve never heard of it before, it is a very cool service you can install on your desktop, tablets, phones and even web browsers to recieve and send push notifications.  Your friends can sign up under one single Push list and send each other notifications or cool links.  You can also use it in your scripts, provided you’d like to dig into the SDK and setup your own framework with the API.

Introducing PowerBullet v 1.0

I present to you the (mostly) finished version of my implementation of PushBullet, which I call PowerBullet.  It currently contains only one function, Send-PushMessage, which will send pushes to all of your devices.  In v 2.0, you’ll be able to specify which device a message goes to.  Currently, you can send the following types of messages:

  • Send a Message/ Note (contains a title, and body text) – works
  • Send an address (Will open in the default maps application) – works
  • Send a file which pre-exists on the web – works
  • Send a link (will open as a URL) – works
  • Send a checklist*
  • Upload and send a file*

There are currently issues with sending a Checklist and Uploading a file.  Due to the more complex nature of these tasks, I plan to fully implement them in v2.0.  For now, the code exists and the function will mostly work, but error out.  If you spot the hole in my logic, let me know!

Download PowerBullet here!

To start off, I need to give credit where credit is due:

  • The API Documentation was pretty good!
  • This post on StackOverflow helped me to understand Multipart forms in JSON
  • Finally, I really began to understand how to present credentials using PowerShell because of this post on ElderRec by Jonathan Gray.  I tried to track him down to give him credit, but couldn’t find his real identity.

Getting set up with PowerBullet

  1. First things first, you’ll need to go to PushBullet.com and register.  Sign up for the free service, then check your e-mail to confirm your account.
  2. Go ahead and register some devices too, you could get the client on your phone or install the extension on FireFox or Chrome.  There is also a desktop client too.
  3. Now, you’ll need to make note of your API key
    Log into PushBullet.  Click your avatar in the upper-right corner, then click Account Settings
    01
  4. Copy the API key listed.   This serves as your login to the web service, and all of your registered devices, channels and subscriptions will be accessed using the key.  You’ll also be tracked by PushBullet using this, so if you start pushing 10,000 messages an hour, you can probably expect to receive an e-mail or phone call about your usage.  Don’t give the API key out to people.
    02
  5. Paste the key into the $APIKey line within the Send-Pushbullet function and get Pushing!

Some examples

Sending a PushBullet Message if a particular error comes up in the Event Log

get-winevent -LogName System | Select -first 1500 | ? Id -eq '1074' | % {Send-PushMessage -Title "Reboot detected on System" -msg ("Reboot detected on " + $_.MachineName + "@ " +  $_.TimeCreated) -Type Message}
03
In this case, look for event 1074, which is a reboot.  This is the Chrome desktop client for PushBullet.

Sending a message containing PowerShell objects

 Send-PushMessage -Type Message -Title "OMG server explode!" -msg "Um, time to come back into the office $Env:COMPUTERNAME died"
04
The $env:ComputerName variable will be automatically swapped for its value. Again in the desktop client.

Send a file which already exists on the web.

Send-PushMessage -FileName "FoxDeploy Logo" -FileType "image/png" -Type File -title "A nice picture" -msg "The FoxDeploy Logo, made by my good friend, Nick Milevsky" -url $url -Verbose
VERBOSE: Sending a file 
VERBOSE: POST https://api.pushbullet.com/v2/pushes with -1-byte payload
VERBOSE: received -1-byte response of content type application/json; charset=utf-8
VERBOSE: OK
SendingAnImage
Here’s an example from the Android Client.

Sending a link

PS C:\temp> Send-PushMessage -Type Link -title "A pretty decent blog" -msg "Some random guys blog" -url http://www.foxdeploy.com -Verbose
VERBOSE: Sending a link
VERBOSE: POST https://api.pushbullet.com/v2/pushes with -1-byte payload
VERBOSE: received -1-byte response of content type application/json; charset=utf-8
VERBOSE: OK
06
Showing the results in the Chrome client. Clicking the button takes you to…
07
Some random guy’s website

Finally, here is an example of embedding PowerBullet within another function. In this case, we’ll make a quick function with a number-indexed hashtable, and invoke Get-Random to get a number. We’ll call this ‘Simulate-DataCenterFault.  If all goes well, this should come up with a random number, then pull the address from the appropriate key-value pair, and then send a PushBullet message with a working address link.

Function Simulate-DataCenterFault {

$random = get-random 4 -Minimum 1

$DataCenterLocations = @{ 1 = "Capitol Lake Park, Olympia, WA 98501"; 
                          2 = "900 Court St NE, Salem, OR 97301"; 
                          3 = "547 L St, Sacramento, CA 95814";
                          4 = "1280 Peachtree St NE, Atlanta, GA 30309"}

"Trouble reported at Datacenter $random"
("Address: "+ $DataCenterLocations[$random])

Send-PushMessage -Type Address -PlaceName "Datacenter $random" -PlaceAddress $DataCenterLocations[$random] -msg "Trouble reported at Datacenter $random"
}

Simulate-DataCenterFault

 

 

0805

Advertisements

4 thoughts on “PushBullet + PowerShell = PowerBullet!

  1. Pat November 4, 2014 / 9:42 pm

    Very nice! I made a slight change and made the $api variable an array called $apis instead, and similarly created a $creds array. I then changed all the actual Pushes to loop through the $creds array.

    I’m not sure how useful these changes might be for others, but I’m thinking of possibly using this to notify a small group of users instead of just me.

    • FoxDeploy November 4, 2014 / 9:46 pm

      There is support for messaging just a particular device in the api. I need to dig into it, and then I’ll upgrade this!

  2. Sergey May 29, 2015 / 2:58 pm

    Very good, fanks.

  3. Sergey May 29, 2015 / 3:00 pm

    Very good…
    I’m find it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s