Powershell - Rest API with XML output

Good morning guys,

I have a simple REST API call that returns XML but for the life of me I can't figure out why the file is coming back empty.

My code:

$key = "&key=b8dedadf7bf2554b636XXXXXXXa42a27affd2a"
$filter = "?status_id=1"
$url = "http://server:3000/redmine/users.xml" + $filter + $key

Invoke-RestMethod $url | out-file "T:\temp\test.xml"

When I run it the file will just contain:

xml                                                                                                        users                                                                                                     
---                                                                                                        -----                                                                                                     
version="1.0" encoding="UTF-8"                                                                             users       

But no other information.

If I issue the REST call into the browser directly,  I get nice clean XML response with all the information I need.

Obviously I am doing something wrong in Powershell but I can't figure it out!

July 3rd, 2013 8:21pm

That is correct.  That is what happens when you "out" the xml object.

$xml=Invoke-RestMethod $url
$xml.Save('T:\temp\test.xml')

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 8:26pm

Yep, I'm an idiot...

Working like a charm now.

On a side note,  I've been googling a way to process the XML without writing to a file, but I've had no luck.  Any idea?

Basically this will return a list of users and I need to extract each user 'ID' number.

July 3rd, 2013 8:32pm

Start here: http://www.w3schools.com/xml/default.asp

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 8:35pm

$xml.SelectNodes('//users')

OR

$xml.users.user

July 3rd, 2013 8:36pm

Second one worked like a charm.  Your a legend!
Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 8:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics