Posts RSS Comments RSS 127 Posts and 199 Comments till now

A Powershell Adventure: Chpt2 ‘Using .NET in Powershell’

Using .NET in Powershell (admins view)
————————————————————
One of the best features of POSH is the direct access to the .NET Object model. Unfortunately… this was one of my biggest obstacles. I’m not a developer and most of the concepts of programming are somewhat foreign to me.

When I started learning POSH I didn’t know the difference between Classes, Interfaces, constructs, or members and I didn’t truly understand the concept of objects (from a programmers point of view.) I think these concepts are critical to learning and using .NET with POSH. I also found that knowing a little c# (at least be able to read it) has become incredible helpful.

I would like to try to explain these concepts without getting to developery. As in all the things I post.. I like to explain by examples so I will be providing some code to help me through the process. I will also try (with my limited ability) to give some basic guidance in C# > POSH translation (this is critically helpful as most .NET examples are written in c#.)

Ok… Lets start with Definitions. Again.. if you’re a Developer… Please feel free to comment on anything I may not be exactly right on. These definitions are my opinion on what these concepts entail.

Definitions
===========
.NET: Blackboxed 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 invaluable resource for this.

Wiki - http://en.wikipedia.org/wiki/Microsoft_.NET_Framework

Class: Almost everything I reference in .NET is a class. I like to think of a class as a template for an Object. A class is definition of what an object should look like. What properties/methods it should have. For Example… a Microsoft.Win32.RegistryKey object should have Name Property and a GetValue method.

Wiki - http://en.wikipedia.org/wiki/Class_%28computer_science%29

Members: Every class has members. Members are the properties and methods combined. It is a good place to look if you just want to see and overview of what a class has to offer. Check this out.
http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey_members.aspx

Properties: Properties are define by a class as attributes of an object. Microsoft.Win32.RegistryKey class has properties of Name, SubkeyCount, and Value Count. So every Microsoft.Win32.RegistryKey object can have those properties.
Wiki- http://en.wikipedia.org/wiki/Property_%28programming%29

Methods: Methods are also defined by the class but instead of an attribute its more like a function of the class. Microsoft.Win32.RegistryKey class has CreateSubKey, DeleteSubkey, SetValue, and so on. So… just like properties every Microsoft.Win32.RegistryKey object can have those methods.

Wiki - http://en.wikipedia.org/wiki/Method_%28computer_science%29

Constructor: I define Contructs as the information or objects required to create an instance of a class. Lets use System.Data.SqlClient.SqlCommand as an example. It has 4 different ways you can create an object from the class. Each way creates and object with slightly different data.
Ref: http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.sqlcommand.aspx

Wiki - http://en.wikipedia.org/wiki/Constructor_%28computer_science%29

Static Methods: These are just like methods, but are availible without having to create an instance of the object. The reason I single these out is because in Powershell… accessing static functions is really simple. All you have to do is to [.NET Class]::StaticMethods(”arguments”). Here is an example [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey.

Now that you know that…

Lets look at some examples of .NET use in POSH.
Lets look at Eventlog access using .NET

[code]PS C:\> $evtLog = new-object system.diagnostics.eventlog
PS C:\> $evtLog.Log = "Application"
PS C:\> $evtLog.Entries
Index Time Type Source EventID Message
----- ---- ---- ------ ------- -------
1350 Nov 19 15:54 Warn Alert Manager Eve... 257 VirusScan Enterprise: Would be blocked by behaviour blocking
1351 Nov 19 15:54 Warn Alert Manager Eve... 257 VirusScan Enterprise: Would be blocked by behaviour blocking[/code]

Lets look at a couple of .NET static method Calls

Now Method of System.DataTime
[code]PS C:\> [system.datetime]::now
Monday, January 08, 2007 7:31:31 PM[/code]

GetLogicalDrives method of System.Environment
[code]PS C:\> [System.Environment]::GetLogicalDrives()
A:\
C:\
D:\
E:\
F:\[/code]

One last one… System.Math. We will use the static method pow. What is 83 to the second power anyway?
[code]PS C:\> [system.math]::pow(83,2)
6889[/code]

C# to Powershell translation
C# Code taking from:
http://www.csharp-station.com:80/Tutorials/AdoDotNet/Lesson07.aspx

[code]
using System;
using System.Data;
using System.Data.SqlClient;
// create and open a connection
objectconn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
conn.Open();
// 1.  create a command object identifying
//     the stored procedure
SqlCommand cmd  = new SqlCommand("CustOrderHist", conn);
// 2. set the command object so it knows
//    to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which
//    will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@CustomerID", custId));
// execute the command
rdr = cmd.ExecuteReader();
// iterate through results, printing each to console
while (rdr.Read()){
   Console.WriteLine(
   "Product: {0,-35} Total: {1,2}",
   rdr["ProductName"],
   rdr["Total"]);
}[/code]

Now in PowerShell:

[code]
$srv = "srv1"
$db = "Northwind"
$conn = new-ObjectSystem.Data.SqlClient.SqlConnection("Server=$srv1;DataBase=$db;IntegratedSecurity=SSPI")
$conn.Open() | out-null
$cmd = new-Object System.Data.SqlClient.SqlCommand("CustOrderHist", $conn)
$cmd.CommandType = [System.Data.CommandType]'StoredProcedure'
$cmd.Parameters.Add("@CustomerID","ANATR") | out-Null
$rdr = $cmd.ExecuteReader()While($rdr.Read()){
    Write-Host "Product Name: " $rdr['ProductName']
}
$conn.Close()
$rdr.Close()[/code]

Ok… Now to explain a couple of differences.

I think the key part of C# to Powershell translation is understanding the Namespace. In Powershell (at least as far as I know) you are unable to include namespaces. Therefore you have Fully Qualify any .Net classes you want to us. This is not the case in C#. In C# you are able to include namespaces. i.e. System.Data.SqlClient. This makes the translation a little more complicated because you have to figure out what namespace the class is from. Lets look at This example.
[code]conn = new SqlConnection(”Server=(local);DataBase=Northwind;Integrated Security=SSPI”);[/code]
How do I know what SqlConnection is? You best option is to google/MSDN it. If that is unavailble you can look that the using statements and see which make sense. In this case it is using System.Data.SqlClient.
So that in PowerShell is
[code]$conn = new-Object
System.Data.SqlClient.SqlConnection(”Server=$srv1;DataBase=$db;Integrated
Security=SSPI”)[/code]

Another important issue is strongly typing. While you can strongly type in Powershell you don’t have to. In my understanding of C# you have to strongly type variables. Not only that but you have instantiate them as well. This illustrated in this line
[code]SqlCommand cmd = new SqlCommand(”CustOrderHist”, conn);[/code]
This line in Powershell is (notice the fully qualified class)
[code]$cmd = new-Object System.Data.SqlClient.SqlCommand(”CustOrderHist”, $conn)[/code]

Summary: As I hope you can see. Using .NET in Powershell is pretty strait forward and actually if you use PowerShell at all… you use .NET regularly, but maybe not as directly. Please feel free to leave comments. Also… please let me know if anything is unclear.

No Responses to “A Powershell Adventure: Chpt2 ‘Using .NET in Powershell’”

  1. on 19 Jan 2007 at 6:20 pmJim V

    Great stuff.

    I like the look of your new blog. It\’s clear and easy to read.

    Keep on truckin\’

  2. on 29 Jan 2007 at 6:28 amsluice

    What we need is the POSH version of Microsoft’s WMI Code Creator and a POSH .NET Code Creator.

  3. [...] recent post title C# to PowerShell Translation Thought Process New!Kill-UserProcess (my first whatif Script) NewWhere is BS? NewUPDATED!!! Get-Uptime (The CustomObject extravaganza!!!) Get-Uptime (fun with Write-Host) Powershell Information Central (work in progress) A Powershell Adventure: Chpt2 'Using .NET in Powershell' Powershelling Citrix (The Good, Bad, and The Code) A Powershell Adventure: Chpt1 'Trial by Fire' Powershell, Remote Registry and You! Part 1 (Overview) WordPress sidebar #wordpress_sidebar div.sidetitle{ padding: 1em 0 0 0; } Categories [...]

  4. on 09 Dec 2007 at 5:04 amAJ

    You’re not a developer?
    Hard to believe. This has to be one of the best descriptions I’ve seen to date on using .Net in Powershell.

    Thanks!

  5. on 09 Dec 2007 at 4:29 pmBrandon

    Thanks AJ!

  6. on 13 Feb 2008 at 6:36 amTerry Nova

    LOVE your blog, thanks for entertaining me
    Hope there will be more posts soon
    regards, terry
    ps - sorry im not that good in writing in english because I came from europe - but i understand a lot

Trackback this post | Feed on Comments to this post

Leave a Reply

CAPTCHA image