powershellobject-graph

How to pass a simple string variable in place of a literal dot notation value?


I am dealing with a situation where some of the properties returned by invoke-restmethod such as link, are nested, this is based on the URI/Feed:

Invoke-RestMethod https://gurneyjourney.blogspot.com/feeds/posts/default |select-object title,link

title link
----- ----
title {link, link, link, link…}
title {link, link, link, link…}
...
...

and some are not:

 Invoke-RestMethod https://hacks.mozilla.org/feed|select-object title, link

title                                                                      link
-----                                                                      ----
Puppeteer Support for the Cross-Browser WebDriver BiDi Standard            https://hacks.mozilla.org/2023/12/puppeteer-webdriver-bidi/
Firefox Developer Edition and Beta: Try out Mozilla’s .deb package!        https://hacks.mozilla.org/2023/11/firefox-developer-edition-and-beta-try-out-mozillas-deb-package/
...
...

I would like to handle each RSS feed/URI in a forEach-Object loop and "un-nest" any nested properties and return objects that conform to the hacks.mozilla.org/ example. How I am thinking of doing this is:

#A small sample of a much larger CSV file
@'     
Url, link_nested
"https://gurneyjourney.blogspot.com/feeds/posts/default",link[4].href
"https://hacks.mozilla.org/feed",0
'@
|convertfrom-csv| forEach-Object {
    $link = $_.link_nested ? $_.link_nested : "link"
    $feed = Invoke-RestMethod $_.url -AllowInsecureRedirect
    $feed.$link            # I also tried --> $feed.($link) ; $feed."$link"  ;  ($feed).$link ; $feed."$($link)"
}

The above does not work. I get no output. What I am doing is no different from the following (I think), which works fine:

$obj = [pscustomobject]@{foo = "foo" ; bar = "bar"}
$thisProperty = "foo"
$obj.$thisProperty

foo       #output

At this point this is really not about the current task I am trying to solve but finding away to pass a dynamic string to dot notation, a capability I am in sore need of.

It has always been a flaky experience, it works sometimes and sometimes not.

I am hoping to conclude this affair. Cheers.


Solution

  • Therefore, use the following - note how the link_nested column in the CSV input data now specifies (nested) property-access expressions rather than mere names or index numbers:

    @'
    Url,link_nested
    "https://gurneyjourney.blogspot.com/feeds/posts/default",.link[4].href
    "https://hacks.mozilla.org/feed",[0].link
    '@ | 
      ConvertFrom-Csv | 
      ForEach-Object {
        $feed = Invoke-RestMethod $_.url -AllowInsecureRedirect
        # Note the following, necessitated by the use of "...":
        #  ` before $feed to prevent its up-front expansion
        #  $(...) enclosure around $_.link_nested to embed its value
        Invoke-Expression "`$feed$($_.link_nested)"
      }
    

    Note that an expandable (interpolating), double-quoted string ("...") is used as Invoke-Expression's input:

    Output:

    http://gurneyjourney.blogspot.com/2023/12/background-painting-with-ice-and-snow.html
    https://hacks.mozilla.org/2023/12/puppeteer-webdriver-bidi/