/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Commandline editing with MSH. "," MSH Scroll-host function "," Series about Monad on Computerworld "," Watching FSRM Quota and filescreen events from Monad "," More ACL and MSH on MSH for Fun "," Jelle "," Monad and "Jeugd sentiment" "," Monad Beta 3 Docs changes "," Get SpecialFolder Locations in Monad "," Filling Emty Properties in AD from MSH "," ")

Saturday, January 21, 2006

 


MSH snap-in to Translate IADsLargeInteger to Int64



As I mentioned before ( Monad(MSH) Beta 3 is getting closer )I was looking for a way to get to LargeInteger values from AD in MSH,

I could not read the Com-Object from MSH, see also this Thread : accesing IADsLargeInteger
As I needed the Logon date from AD that is a LargeInteger Value.

I still did not find a way, but since MSH now supports snap-in's I decided to write one for it.

Get-IADsLargeInteger

I't will cast the IADsLargeInteger COM-object to a Long (Int64) so we can use it from MSH, from there it's easy to get the Date,

So now I can get to the PwdLastset property like this (as I'm using Adam for testing, this accounts Logon values are not filled, that's why I use that in this example)

MSH>$de = New System.directoryservices.DirectoryEntry("LDAP://localhost:389/cn=mow,ou=mowOu,dc=mow,dc=adam")
MSH>$li = $de.get_Properties()["pwdlastset"].Value
MSH>Get-IADsLargeInteger $li
127770055020000000
MSH>$long = Get-IADsLargeInteger $li
MSH>[datetime]::FromFileTime($long)

maandag 21 november 2005 1:11:42


Let's get to the making of the Snap-in,
I First made the Snap-in in C#, following the example in the getting started.

it looks like this :

//getIADsLargeInteger.cs
// MSH snapin to get an IADsLargeInteger object to an Int64
// /\/\o\/\/ 2006
// http://mow001.blogspot.com

using System;
using System.ComponentModel;
using System.Management.Automation;
using System.Reflection;

namespace mow
{
    // This class defines the properties of a snap-in
    [RunInstaller(true)]
    public class getIADsLargeInteger : MshSnapIn
    {
        /// Creates instance of DemonSnapin class.
        public getIADsLargeInteger() : base() { }
        ///Snapin name is used for registration
        public override string Name
        { get { return "getIADsLargeInteger"; } }
        /// Gets vendor of the snap-in.
        public override string Vendor
        { get { return "MOW (http://mow001.blogspot.com)"; } }
        /// Gets description of the snap-in. 
        public override string Description
        { get { return "Gets a IADsLargeInteger"; } }
    }
    /// Gets a IADsLargeInteger
    [Cmdlet("Get""IADsLargeInteger")]
    public class getIADsLargeIntegerCommand : Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true)]
        public object  LargeInteger
        {
            get { return largeInt; }
            set { largeInt = value; }
        }

        private object largeInt;

        protected override void EndProcessing()
        {
            ActiveDs.IADsLargeInteger li = (ActiveDs.IADsLargeInteger)largeInt ;
            long lng = (long)((uint)li.LowPart +(((long)li.HighPart) << 32));
            WriteObject(lng);
        }
    }
}


But how to get this into MSH as a working snap-in, this takes a few steps.

First save the C# code above in getIADsLargeInteger.cs, in the MSH program directory,
e.g. : "C:\Program Files\Microsoft Command Shell\v1.0"

then, It uses the ADSI typelibrary to "translate" the ADSI Com-object so We need to provide that also, it is the following DLL,

Interop.ActiveDs.dll

the most easy way is to get this DLL, is to start a project in VS 2005 and add the ADSI typelibrary as a reference.( Project -> Add reference -> COM)

if you then build the project the DLL is generated in the Bin directory, also you can use the commandline too tblImp for this.

Copy this DLL to the Monad Directory also.
Now we can past the following code into the MSH Console to compile the Snap-in :

cd $MSHHOME

$compiler = "$env:windir/Microsoft.NET/Framework/v2.0.50727/csc"
$ref = "$MSHHOME/System.Management.Automation.dll"
$ref2 = "$MSHHOME/Interop.ActiveDs.dll"
&$compiler /target:library /r:$ref /r:$ref2 getIADsLargeInteger.cs


Now a DLL is created, getIADsLargeInteger.dll

Now we need to install it first with the installutil,
and then we can add it to the MSH-shell.

# install Snap-in

set-alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutil
installutil getIADsLargeInteger.dll

# Load the Snapin

add-mshsnapin getIADsLargeInteger


And now we are ready to use the Snap-in as in the Example we started with, and are able to get the Dates we need from AD.
and as this snap-in is loaded in MSH now, it will even show up in get-command :

Cmdlet Get-IADsLargeInteger Get-IADsLargeInteger [-LargeInteger] Object [-Verbose] [-D...

and Get-IA [Tab] will work also !!

I still hope there will be a way to do this from MSH, I can load the typelibrary, I do not know why the same trick does not work in MSH,
but this will get me going, and making my first Snap-in in MSH was also a nice adventure, as this was also my first C# project.

gr /\/\o\/\/
Tags :


Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?