This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
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 IADsLargeIntegerAs 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-IADsLargeIntegerI'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 : Monad msh