August 4, 2014 at 10:22 am #17758
So sometime in the last month or two Dell has made changes to their API. I had been using a script posted to poshcode to gather warranty information on all our workstations but that no longer works. So I figured I'd take a stab at writing it myself. Problem is, I know jack crap about parsing XML with PowerShell. I'm beyond frustrated and just looking for some guidance before I hulk-smash my desk.
Here is the uri I'm using (complete with service tag and api key): https://api.dell.com/support/v2/assetinfo/warranty/tags.xml?svctags=1763DX1&apikey=1adecee8a60444738f280aad1cd87d0e. Plug that into a browser and I get pretty pretty XML.
If I run (Invoke-WebRequest https://api.dell.com/support/v2/assetinfo/warranty/tags.xml?svctags=1763DX1&apikey=1adecee8a60444738f280aad1cd87d0e).Content it returns the XML, but I can't figure out how to pull out the information I need – namely the warranty StartDate and EndDate.
August 4, 2014 at 11:19 am #17759
I'm learning about parsing XML at the moment also. I believe you will need to write an XPath expression and that will allow you to grab the data you need.
August 4, 2014 at 1:04 pm #17766
Does this work for you ?
$DellResponse = Invoke-WebRequest 'https://api.dell.com/support/v2/assetinfo/warranty/tags.xml?svctags=1763DX1&apikey=1adecee8a60444738f280aad1cd87d0e' $xml = [xml]$DellResponse #The object way $WarrantyDetails = $xml.GetAssetWarrantyResponse.GetAssetWarrantyResult.Response.DellAsset.Warranties.Warranty $WarrantyEndDates = $WarrantyDetails.EndDate $WarrantyStartDates = $WarrantyDetails.StartDate #Or the XPath way $WarrantyDetails = $xml.SelectNodes("*/*/*/*/*").Warranty $WarrantyEndDates = $xml.SelectNodes("*/*/*/*/*/*").EndDate $WarrantyStartDates = $xml.SelectNodes("*/*/*/*/*/*").StartDate "Warranty details:" $WarrantyDetails "`nWarranty End Dates:" $WarrantyEndDates "`nWarranty Start Dates:" $WarrantyStartDates Warranty details: EndDate : 2016-04-26T23:59:59 EntitlementType : EXTENDED ItemNumber : 939-7368 ServiceLevelCode : ND ServiceLevelDescription : Next Business Day Support ServiceLevelGroup : 5 ServiceProvider : UNY StartDate : 2014-04-27T00:00:00 EndDate : 2016-04-26T23:59:59 EntitlementType : EXTENDED ItemNumber : 939-7738 ServiceLevelCode : TS ServiceLevelDescription : Client Gold Support / ProSupport ServiceLevelGroup : 8 ServiceProvider : DELL StartDate : 2014-04-27T00:00:00 EndDate : 2014-04-26T23:59:59 EntitlementType : INITIAL ItemNumber : 939-6888 ServiceLevelCode : ND ServiceLevelDescription : Next Business Day Support ServiceLevelGroup : 5 ServiceProvider : UNY StartDate : 2013-04-26T00:00:00 EndDate : 2014-04-26T23:59:59 EntitlementType : INITIAL ItemNumber : 939-7718 ServiceLevelCode : TS ServiceLevelDescription : Client Gold Support / ProSupport ServiceLevelGroup : 8 ServiceProvider : DELL StartDate : 2013-04-26T00:00:00 Warranty End Dates: 2016-04-26T23:59:59 2016-04-26T23:59:59 2014-04-26T23:59:59 2014-04-26T23:59:59 Warranty Start Dates: 2014-04-27T00:00:00 2014-04-27T00:00:00 2013-04-26T00:00:00 2013-04-26T00:00:00
For XPath examples you may like to see the following webpage http://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx
I am learning myself XPath syntax so surely the proposal is far from perfect.
August 4, 2014 at 1:13 pm #17767
Good work Tomasz, Looks so easy once someone else has done it 🙂
I tried to put this together for a little bit. When I ran $xml i got the following and it i thought i was going down the wrong path and gave up since i couldn't see any further info, didnt think to query it further.
PS C:\windows\system32> $xml
August 4, 2014 at 11:36 pm #17774
I tend to use get-member quite often when dealing with objects I am not familiar with.
So in this case you would run the following to get more info about the xml object
$xml | gm
August 4, 2014 at 11:42 pm #17775
I look at it now and that would be the obvious thing to of tried. Since its XML for some reason i think that threw me off and i didn't think could try that. Going to try working with XML more as i do think its really useful for more complex data structures.
Anyway, I know for next time now 🙂
August 5, 2014 at 4:24 am #17783
That is fantastic!! I spent most of yesterday trying to figure out how to use xpath to do what I wanted. It never occurred to me to use wildcards. I kept getting stuck at $XML.SelectNodes("/GetAssetWarrantyResponse") and couldn't drill down to the sub-elements.
I think I prefer the object method. It seems so much more straight forward, and obvious now that I see it. 🙂
One question – Is there any benefit to using one over the other? The object vs xpath?
August 5, 2014 at 8:56 am #17793
I am happy it worked for you.
The other day when struggling with xm doc , I came across the following article. [url]https://www.simple-talk.com/content/article.aspx?article=1917[/url]
The author made quite a nice study between xml object and Xpath methods.
Let me quote the extract:
[blockquote]"Comparison of XPath and Object Approaches
Which approach is better to access XML data? Table 1 helps you answer this question. The object approach is usually more concise (e.g. line 3), but not always (line 4). XPath, however, is more expressive, in that it allows you to specify some selectors that are not possible with object notation (line 7). However, PowerShell's own capabilities can easily fill this gap when using object notation (line 8)."[/blockquote]
Personally I do not mind either as long as the job is done.
I guess the difference is when working at scale with a lot of data processing. Then small performance advantage from one method over the other one, can have a huge difference in long run..
You would apply measure-command to find out what works better for you.
You must be logged in to reply to this topic.