Well it had been an interesting few weeks and in my current house we have just had a new housemate move in who fits in really well with the rest of us.
We discussed in the initial few weeks what we all did and naturally it brought up that I work within IT and enjoy (when I can) to have a few games on the 360 – Mainly Call Of Duty and a few others (Guitar Hero is another favourite)
Naturally as time progress this brought up a state of competition when it comes to Xbox – He is amazing at Guitar Hero and I’m slowly getting better myself too.
However the point in this post is to detail about what he was upto currently.
So as we came into May we had discussed about Xbox 360 and Xbox One games and he told me that he was starting a Gamerscore Competition with a friend and that he wasn’t sure on who was currently winning.
So I said to him “I could write a script to track this for you and then email you with an update on whether you are winning or loosing and by how much”
His response was almost priceless – ” Really can that actually be achieved!!!”
My response was of course it can – I’ll just use my IT Techy Skills and we will see what we can achieve.
So as per my usual stance with things like this I get a few further requirements from him and these included
- Getting a daily Email with the current standing – Who was winning and by how much
- A daily tracker of the amount of Gamerscore achieved each day – this was for low level trend analysis.
- The other guy wasn’t to be made aware of this tracking that was ongoing.
So I set off and looked about to see if there was a suitable Xbox Live API for pulling back Gamertag information and was surprised that there wasn’t an official one – Come on Microsoft!!!!
So once I realised that I couldn’t get the information via an API it was then apparent that I would need to move onto using Invoke-WebRequest to do the heavy work – A shout out to Heath Groves @Heath_Groves for demoing Invoke-WebRequest to me at the SharePoint User Group North West Developer Day in December 2014 (feels so long ago now)
So now we know the Method to get the information there are some standard Variables that we needed to define and these included
- Starting Gamerscore – As they hadn’t used new Gamertags which meant the tracking had a starting point from which to calculate from.
- SharePoint Site and List and Fields needed – I wouldn’t be a SharePoint Guy if I didn’t make use of SharePoint for this somewhere haha
- Which Site we will be pulling the information from as until I can figure out passing my Xbox/Hotmail credentials via Invoke-WebRequest to xbox.com I cannot make use of that site. (Only spent 2 minutes on this so will revisit another time)
- Email Address to send from
- Email Address to send to
- Smtp Server to send the message via
- Any needed Credentials to connect to any of the services as required – in this example I connect to my Office 365 Tenant for both the Email and SharePoint Site so I need to pass that credential across.
So I have all the Variables I need – so really how difficult is this in being able to get the information that I want and to then manipulate it correctly.
So I had decided on which Site I wanted to get this information from and now the key thing I needed from this point was to get the exact point where this information is stored
I chose to use www.xboxgamertag.com to pull the information so I then needed to find the table used in the Site page that holds the GamerScore as can be seen below
And in a HTML view this is shown as the below
So to actually get this across from the Site into PowerShell we then will be running the following lines of PowerShell
The Key to this is that we have got all the HMTL for the Page by running Invoke-WebRequest and have then stored this in a Variable $gt1html
We then on the next line where we will perform the manipulation of the Html to get the data we actually require. This is rather simply given to us by the Unique Element & Class used to style and provide the content to the Web Browser (although it could have been done all in 1 line instead of splitting into 2 here – however for readability I feel this is a better practice to get into for larger scripts)
So as you can see I have to Get all the Elements that have the Tag Name of “P” once I have this I then Pipe that across to Where-Object and specify at this point that I want back the Content that coincides with the Classname of “rightGS” and to do that as seen above I have encapsulated the whole query within the Brackets – in this case the 1st set – this is so that I can then ask for the innertext of that cell and as the data came back in a String Format with a space in front of the first number and a comma I have run a replace action to replace these unneeded punctuation to just give me the number that I need.
Once this is done I then store all that in a new Variable that I have hard defined to be a INT32 this is just for neatness but it easily could have stayed in a String format.
Note at this point I already had a list created however for completeness I will create a new one with the script that you will be able to download
So from here to do the daily reporting on the updates to the GamerScore we need to Create an Item in a List with the starting points and to make this ambiguous I have thrown this all together into 1 single script with some amendable Variables which are all included at the top of the Script which will check if the list exists and if not will create it and also populate with a baseline item
To adapt this for yourself you will need to change the following Variables / Functions
- To Email Address
- From Email Address
- CC Email Address – If you require it
- SMTP Server Details
- List title within the SharePoint Site
- GamerTag 1 Starting GamerScore
- GamerTag 2 Starting GamerScore
- Create-MyO365Creds – See this as an example of the type of credential you will need to provide also see this Post as detailed in a Previous Blog Post Secure Password with PowerShell: Encrypting Credentials – Part 1
You can download the full Script from the following Xbox Live GamerScore Tracker Script
Once again you can follow me on Twitter – @ryanyates1990
Or reach out to me via the Office 365 Yammer Network – https://www.yammer.com/itpronetwork
* Please note – This was very quickly put together and doesn’t accommodate for Special Character encoding like spaces which in HTML is %20 or in FieldNames is _x0020_ *