Keep it Simple

// a software developer's blog by Karthikeyan Vijayakumar

Using C# code in Powershell

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.

Continue reading

HTMLAgilityPack Rocks

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.

Continue reading

What really happens in a try – return – finally statement?

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:

  1. Code before return statement is executed
  2. Expression in return statement is evaluated
  3. finally block is executed
  4. Result evaluated in step#2 is returned

Continue reading

Download images from a URL in C#

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.

Continue reading

« Older posts

© 2016 Keep it Simple

Theme by Anders NorenUp ↑