Powershell – Accessing DotNet Assemblies

As you all know Powershell is built on top on the .Net Framework. Which contains a lot of classes that we could benefit from if we could access them in our scripting environment.

Well we can using the Assembly Reflection method.

There are different ways of accessing .NET Assemblies depending on the version of PS

PS Version 1

[Reflection.Assembly]::Load Loads an assembly
[Reflection.Assembly]::LoadFile Loads the contents of an assembly file on the specified path
[Reflection.Assembly]::LoadFrom Loads an assembly given its file name or path
[Reflection.Assembly]::LoadWithPartialName Loads an assembly from the application directory or from the global assembly cache using a partial name

Example :

[Reflection.Assembly]::LoadFile( `
  'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Web.dll')`
  | out-null            

[System.Web.HttpUtility]::UrlEncode("this is a test")

</br>PS Version 2

Using the Add-Type Cmdlet

Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::UrlEncode("this is a test")

These 2 examples use the URLEncode method in the HttpUtility Class.

Question is now which Classes are available (Public) to PS.

Well you can use this code to access Assembly Information.

[appdomain]::currentdomain.GetAssemblies() | % {$_.gettypes()}
| sort basetype | Out-GridView

And filter it in you the GRID

image

Now filter for Public Classes

image

Great playing field for Scripters !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: