Introduction
This week has gotten away from me, but I wanted to post a few blog posts to round out this week. For today’s post, this one will be a short post, as it is a Quick PowerShell Post … I hope you will find it interesting and possibly useful. Conception of the idea for today’s post was a way to find my latest blog posts with PowerShell and compress all information into a simple. List. With a bit of searching the Internet for some help on the concept, I was able to find a similar idea, but the code I found did not list blog articles. However, it provided enough of a clue. Let’s walk through the steps I took to get from beginning to end and then apply what we learned to other websites.
Simple Query
To query a website with PowerShell, we can use Invoke-WebRequest which will pull web data from any URL you specify with the cmdlet.
Invoke-WebRequest -Uri https://www.powershellgeek.com
(sample results below):
The important part is in this section:
Links
Is we want, we can now take those links and list them all with this one-liner:
(Invoke-WebRequest -Uri https://www.powershellgeek.com).Links
Breaking down the results, we see two types of ‘Links’ that are listed.
Blog Articles
Misc Links
Note that both of these have a ‘rel’ property value of ‘Bookmark’ however, the different is in the ‘innerHTML’ property value. On the links we want, the article title is listed, but the ones that do not have good title text, we can just filter our any bookmarks that have a property value starting with a ‘<‘ for the InnerHTML property.
Now that we have all the parts of the one-liner, we can assemble it like so:
[sourcecode language=”powershell”]
(Invoke-WebRequest -Uri https://www.powershellgeek.com -Method GET).Links | Where rel -eq bookmark | Where innerHTML -notlike ‘<*’
[/sourcecode]
We now have this:
As can see, the InnerHTML property contains the name of the article. Now that we have all of the criteria, let’s create our one-liner.
Complete One-Liner
Now that we have all the parts of the one-liner, we can assemble it like so:
(Invoke-WebRequest -Uri https://www.powershellgeek.com -Method GET).Links ` | Where-Object rel -eq bookmark | Where-Object innerHTML -notlike '<*' | ` Format-Table @{Expression ={$_.innerHTML};label='Blog Post'}
Other Websites?
Assuming can potentially be a bad this, but let us see what we can do if we use the same one-liner on other blog based websites?
MVP Website 1 – https://eightwone.com/
Looks like it did indeed work with this website:
MVP Website 2 – https://blog.expta.com/
… and failure … Which just means some backtracking and recoding the script. If you feel adventurous, see what you can come up with to resolve this issue and post your code in the comments. Hint, Bookmarks are still there, but no titles are present in the Bookmark entries.
MVP Website 3 – https://www.michev.info/
… and this one works as well …
Conclusion
PowerShell is powerful. /cliche
In all seriousness, the Invoke-WebRequest does indeed show the power of the language and it’s versatility. Maybe you do not need a script to pull the latest posts from Microsoft MVP blogs, but they could be useful to those still learning new technologies. To further enhance this, we could query these pages and report back the title, link and date they were created and then email those who need them when a new article is noticed. Just an idea to ponder…
Comments? Questions?
Feel free to leave your Comments below! Learn to more efficiently utilize PowerShell to manage Exchange Server, Exchange Online, Microsoft Defender for Office or Microsoft Purview Compliance portals by picking up frequently updated eBooks: