Last weekend I attended Philly Code Camp hosted by the Philly .NET User Group. I went to some great sessions on Windows Phone/Mobile development by Nick Landry and Atley Hunter, and I also went to a very informative session on PowerShell led by MVP Doug Finke. I wanted to share a tip I learned there that I think will be useful to other Windows Phone developers.
The biggest ‘wow’ feature of PowerShell for me was the ability to query REST web services from the command line. So today I’ll be showing an example of querying forecast.io from PowerShell. I’ve just submitted an update to my app Car Starter that will add weather forecasts to the app; and while developing that feature I found it was very useful to use PowerShell to explore the response from the forecast.io web service.
To get started open up PowerShell. It comes built-in with the Windows 7 and Windows 8 OS. I’ll assume that you’re using Windows 8 or 8.1 as it’s required for a Windows Phone 8 Dev environment. Just press the Start button, type PowerShell, hit enter and you should be good to go!
We’re going to start by creating an object with our Uri:
Notice the dollar sign infront of the variable name, all variables follow this syntax. Next we invoke the rest method and assign the result to another variable:
Now that you have the $weather object you can easily explore how this service returns a weather forecast.
Typing the object name will print out all the properties of the object as key/value pairs.
You can also drill down into these properties using PowerShell’s dot notation:
And look at individual objects in a large array:
And that’s it! Obviously there is a LOT more you can do with PowerShell, but I just wanted to show one of the most interesting and useful features I learned about at the Philly CC session. If you’d like to dig in deeper with PowerShell I’d encourage you to check out Doug Finke’s website and blog. He’s also the author of Windows PowerShell for Developers which is a fantastic resource.