It would be great if the existing C# code could be reused inside Powershell without a need to implement it as Cmdlet.
And indeed the Powershell Version 2 provides a way to achieve this using the Add-Type Cmdlet which allows to generate a new .NET assembly using the provided C# source code in memory which can then be used by powershell scripts executed in the same session.
I had to do some deep data extraction from a SharePoint List, and naturally leaned on PowerShell for some assistance. PowerShell is a great language for text munging, and web content is no different.
At that point, I remembered something called the HTML Agility Pack that I’ve been meaning to experiment with for some time. The HTML Agility Pack lets you navigate an HTML document as though it were well-formed XML, even though the underlying HTML usually isn’t. It doesn’t leverage PowerShell’s XML adapter, but the .NET objects act just like the XML classes from the .NET Framework.
On the down-side, data navigation and selection in XML comes via the XPath language. Like Regular Expressions, XPath queries are an esoteric art and difficult to get right. Luckily, you don’t need much knowledge of XPath for simple XML navigation.
Have you ever wonder, what really happens in a try – return – finally statement? The finally statement is executed, but the return value isn’t affected. The execution order is:
- Code before return statement is executed
- Expression in return statement is evaluated
- finally block is executed
- Result evaluated in step#2 is returned
You may use the following method to retrieve the image from a specific URL on the web. The below C# function takes an image url, download the image from the url as a stream.