Windows PowerShell and the .NET Framework: Connecting the dots

You don’t have to be a developer to understand how .NET integrates with Windows PowerShell. By learning just a few basic concepts, admins can help put .NET within reach.

One of the best Windows PowerShell features is its direct access to the .NET object model. Unfortunately, this was one of my biggest obstacles when learning PowerShell since I’m not a developer and most of the programming concepts were somewhat foreign to me.

When I first started working with PowerShell, I didn’t know the differences among classes, interfaces, constructs, or members, nor did I truly understand the concept of objects (from a programmer’s point of view). But I do think these concepts are critical to learning and using .NET with Windows PowerShell. I’ve also found that knowing a little C# (or at least being able to read it) is incredibly helpful.

Therefore, I will attempt to explain these concepts briefly through examples and provide some code to help you through the process. I’ll also try (with my limited ability) to provide some basic guidance in C# PowerShell translation.

Let’s start with definitions. Again, if you’re a developer please feel free to comment on anything I may be off about. These definitions are my opinion alone on what these concepts entail.

  • .NET: It is a framework of black-boxed code that accepts specific input and returns either a value or object. Basically, Microsoft did all the coding for you -- you just have to call on it correctly. MSDN is an invaluable resource for the .NET Framework.

  • Class: Almost everything I reference in .NET is a class. I like to think of a class as a template for what an object should look like and what properties and methods it should have. For example, a Microsoft.Win32.RegistryKey object should have a Name Property and a GetValue method.

  • Members: Every class has members, which are properties and methods combined. It’s a good place to look if you just want to see an overview of what a class has to offer.

  • Properties: Properties are one of the two types of members a class can have. Simply put, they can be thought of as attributes of an object. For example, Microsoft.Win32.RegistryKey class has properties like Name, SubKeyCount and Value Count, so every Microsoft.Win32.RegistryKey object can have those properties.

  • Methods: Methods are one of the two types of members a class can have. Like properties, methods are also defined by the class, but instead of an attribute, they are more like a function of the class. Microsoft.Win32.RegistryKey class has CreateSubKey, DeleteSubkey, SetValue and so on. As with properties, every Microsoft.Win32.RegistryKey object can have those methods.

  • Constructor: A constructor is a method of a class that collects the information required to create an instance of that class. I like to define constructs as the information or objects required to make an object useful. Let’s use System.Data.SqlClient.SqlCommand as an example. It has four different ways you can create an object from the class. Each one creates an object with slightly different data.

  • Static fields: These are just like properties, but are available without having to create an instance of the object. In PowerShell, accessing static members is really simple. All you have to do is use [.NET Class]::StaticField.

    In the following example, the .NET class System.Math will return the value of PI:
    [System.Math]::PI.
  • Static methods: These are just like methods, but are available without having to create an instance of the object. Similar to how you access static fields, all you have to do is use [.NET Class]::StaticMethods(“arguments”).

    In the following example, Microsoft.Win32.RegistryKey will return a Microsoft.Win32.RegistryKey object for a remote machine called MyServer.
    [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,”MyServer”)

Now that we’ve covered the basics of .NET Framework, let’s go through some of the PowerShell specifics like creating classes and discovering what properties and methods are available for those classes.

  • New-Object -- Creates an instance of a Microsoft .NET Framework or COM object.

    In the following example I am creating an instance of System.DirectoryServices.ActiveDirectory.DirectoryContext:
    $Context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("DirectoryServer",$Name)
  • Get-Member -- Accesses the properties and methods of objects.

    The following example will return all the members of the $Context object:
    $Context | Get-Member

Putting the .NET puzzle together
Here is an example of .NET at work. It returns Active Directory domain controller objects using new-object, constructors and static methods.

function Get-DC
{
    Param($Name,$Domain)
    

if($Name)
    {
        $Context = new-object
System.DirectoryServices.
ActiveDirectory.DirectoryContext

("DirectoryServer",$Name)

[System.DirectoryServices.
ActiveDirectory.DomainController]
::GetDomainController($Context)
    }
    if($Domain)
    {
        $Context = new-object
 System.DirectoryServices.
ActiveDirectory.DirectoryContext
("Domain",$Domain)

[System.DirectoryServices.ActiveDirectory.
DomainController]::FindAll($Context)
    }
    if(!$Name -and !$Domain)
    {
        $DCName = ([adsi]"LDAP://rootDSE")
.dnsHostname.ToString()
        $Context = new-object
System.DirectoryServices.ActiveDirectory.
DirectoryContext("DirectoryServer",$DCName)

[System.DirectoryServices.ActiveDirectory.
DomainController]::GetDomainController($Context)
    }
}

You can follow SearchWindowsServer.com on Twitter @WindowsTT.

Miss a column? Check out our Scripting School archive.

ABOUT THE AUTHOR
Brandon Shell has been in the IT industry since 1994. He started out as a PC tech and general fix-it guy for numerous companies. In 2007, he joined the PowerShell MVP ranks, and Shell has spent the past several years building his PowerShell knowledge and helping others build theirs.

This was first published in February 2011

Dig deeper on Windows PowerShell Scripting

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close