The Xbox Connection – Part 3

Thanks for joining me for this third and final article on using Powershell to get Xbox Live information.  In the first article, I introduced you to the Xbox web service API and showed how to get information on a user profile from the Xbox system.  In the second article, I showed how to get information on friends and games on your profile.  Now in this final article, I will show you how to get information on individual game achievements.

I still have the data from the last session loaded in memory, so I won’t review how to access the web service again.  As you may recall, there was a property that we did not use last time: AchievementInfo.

gameslistTo get the achievement information, we will start like we have before:

$achievements = [xml]($games.Data.Games | select -first 5 | %{invoke-webrequest -uri $_.AchievementInfo} | select -expand content)


OH NO!  What happened?  We get this really ugly error when we try this code.

gameinfoerrorIf you look back at the former screenshot and examine the AchievementInfo URL, you will see that it does not have a return format specified (XML).  So, by default, the web service will return a JSON response.  While this is a bit of a hiccup, it is not a show-stopper.  Powershell can just as easily chew through a JSON response as it can XML.  We just need to modify our command slightly to account for this.

$achievements = $games.Data.Games | select-object -first 5 | foreach-object{Invoke-WebRequest -URI $_.AchievementInfo} | Select-Object -ExpandProperty Content | ConvertFrom-JSON

All we changed is to take each of the Content properties and pipe it into the ConvertFrom-JSON cmdlet and remove the XML type accelerator.  Now when we look at the $achievements variable, we have all the achievements, earned or not.

achievementsThis is a truncated list and scrolls for a little bit since there were at least three games on my list with achievements available.  The information shown gives you the name, description, associated score, and when and if it has been earned on the specified gamertag.

Now that we have this data in our variable, we can get some good information.  I think I would like to know what achievements I don’t have that will give me the most gamer score.  Here is the Powershell code to do that with my current information from the last five games I have played:

$achievements.Achievements | Where-Object{!$_.EarnedOn} | select-object Name, Score | sort-object Score -Descending | ft -AutoSize

topachievements

Based on this information, four of the top five scoring achievements will come from Halo 4.  I guess I need to stop blogging and get to work!

I hope that you have learned something from these and that you have some fun with the API.  I have enjoyed my gaming, but I have also really enjoyed being able to use one of my favorite technologies and integrate the two.

Advertisements
This entry was posted in Xbox 360, XML and tagged , , , , . Bookmark the permalink.

One Response to The Xbox Connection – Part 3

  1. Pingback: Building an Xbox Live Widget using PowerShell | Learn Powershell | Achieve More

Comments are closed.