Dealing with RSS feeds

I am trying to debug someone else’s PS and would like to know why this would produce a collection instead of just the first RSS “item” in the feed? Where $rssItems is the new collection. I would think you would have to iterate over $content.rss.channel to get all the children (items).

[xml]$content = Invoke-WebRequest -Uri $env:CampusGroupsFeedUrl -ErrorAction Stop $rssItems = $content.rss.channel.item

Hey there and welcome,

Best guess, it seems like the property item actually has multiple objects underneath it. What does the data look like when you display it? We need more info :slight_smile:

RSS feeds can be different and have different data/properties. Have you ran get member on RSSItems to see what it shows? does it show System.Object[]?

I am sorry, I thought I explained this better. The RSS “item” elements are all direct children of the $content.rss.channel element, so all items are siblings of each other directly within the “channel”. Item has its nested children of corresponding data. Here is the actual RSS feed URL. Thanks for your help!

https://rosehulman.campusgroups.com/rss_events

confirmed, that property has multiple items, which is why. I think in this case it’s a naming thing, and that’s what the property is called.

[xml]$Data = Invoke-WebRequest -Uri 'https://rosehulman.campusgroups.com/rss_events'
($data.rss.channel.item).count

outputs
236

Honestly… i Prefer using Invoke-RestMethod for this kind of thing anyway. Give that a shot? it seems like that will just give you the data directly.

$Content = Invoke-RestMethod -Uri 'https://rosehulman.campusgroups.com/rss_events'

Should output those events immediately without the need to go through the properties. It does more work behind the scenes to get you the intended data.
also great to see another indiana person (assuming anyway…) :slight_smile: I work for higher ed in Indiana too :).

1 Like

Thanks! I am fairly new at PowerShell, so it seems odd to me to only drill down to the “item” property and get an entire collection instead of just the first item. Normally, I would iterate over the .channel collection and push them into an array. Is that exclusive to PS?

I’m not sure, tbh - maybe? I mostly only know PS sorry!

From my limited perspective - it has to do with the XML, but I don’t have the experience to talk intelligently about other programming languages. I wasn’t classically taught in programming.

For this, the ‘Item’ (I know its singular) property is named htat way, because of how the XML is. It’s a ‘collection’, as it contains multiple ‘sub-objects’. The ‘channel’ property does not. Channel is only a ‘single’ object, that has multiple properties, one being Item.

($data.rss.channel).count

outputs a count of 1.

If you look at the channel,
You can see what PS is doing by looking at the data in a web browser:

there are multiple item nodes. its turned that into a property based on the name in the XML.

Another way to just mess around with it is this simple code:

$test = [PSCustomObject]@{}
$Test | Add-Member -Name 'Property1' -Value @(1,2,3,4)

There’s only ‘1’ root object, but I added a property named Property1, and It is an array of numbers. each item is an INT, but it’s a system.object or collection of objects if I look at it in the whole. There’s still only 1 root object though. The same principal applies here.

Take a look at: about Member-Access Enumeration - PowerShell | Microsoft Learn as that might add some more context.

So confusing! I can literally see that there are multiple item(s) within “channel”. I guess I’ll just have to accept that this doesn’t make sense.

Your link about Member-Access Enumeration did clear some things up. I wasn’t totally thinking incorrectly, since prior to PS 3.0 you would have to enumerate through the list items instead. This is a good explanation. Thanks for your help!

1 Like