ADSI .NET references and sample code using the LDAP provider

There are some resources on the internet that finally address using .NET and connecting to resources using ADSI.  Here are a few links that I recently used for writing an application that will query Active Directory using the .NET and the LDAP provider.  As the data is queried, I put the data into an in memory datatable and finally commited to a database.   This is my first experience with the LDAP provider, since I really need to get under the hood on dealing with this provider vs the older WinNT provider because of the active directory work i’m doing..   This sample code is something I came up with. It recurses the entire directory starting at my fake domain name called  Check out the MS link listed below, this will give you more examples of how to use the .Filter property.   This isn’t anything real hard but thought I’d share, once this is entirely completed I’ll post up what has been completed.  This entire exercise querys the Win32_NetworkAdapterConfiguration WMI class.   Note this code doesn’t show it but here is the WMI query I’m returning.  “SELECT DNSHostName, DefaultIPGateway, DNSServerSearchOrder, DNSDomainSuffixSearchOrder, IPAddress, IPSubnet, MACAddress, WINSPrimaryServer, WINSSecondaryServer FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True”  The real hard thing dealing with this Win32 Class is many of these items datatype are string of array.

Imports System.Management
Imports System.DirectoryServices
Imports System.Net
Imports System.Text

Module Module1
    Sub Main()
        Dim strUID As String = “domainuserid”
        Dim strPWD As String = “password”
        ListComputers(strUID, strPWD)
    End Sub

    Sub ListComputers(ByVal strUID As String, ByVal strPWD As String)
        Dim strPath As String = “LDAP://DC=Steve,DC=Schofield,DC=com
            Dim objDomain As DirectoryEntry = New DirectoryEntry(strPath)
            Dim objOU As DirectoryEntries = objDomain.Children
            Dim objOUName As DirectoryEntry
            Dim strSubPath As String

            For Each objOUName In objOU
                strSubPath = objOUName.Name.ToString() & “,DC=Steve,DC=Schofield,DC=com”
        Catch f As Exception
        End Try
    End Sub

    Sub GetServerName(ByVal strPath As String)
            Dim enTry As DirectoryEntry = New DirectoryEntry(“LDAP://” & strPath)
            Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
            mySearcher.Filter = “(&(objectClass=computer))”
            Dim resEnt As SearchResult
            Dim rowcomputer As DataRow
                For Each resEnt In mySearcher.FindAll()
            Catch f As Exception
            End Try
        Catch f As Exception
        End Try
    End Sub
End Module

Listing All Computers in Active Directory

Directory Searcher Class

ADSI Scripts resource

Executing an EXE inside a VBScript file that has spaces in the path.

I’ve done quite a bit of scripting but ran across something that baffles me.  Trying to execute a file in a directory with a spaces in the path would bomb.  I googled and found a thing that has me put [] inside to fix it but it didn’t work.  Here is what i was trying to do, if there is no spaces in the path to the EXE including any directories, the script works fine.   Here is an example to a path for an EXE c:winntsystem32Ica PassThroughpn.exe.  Here is the script code

Set wshShell = WScript.CreateObject (“”) “c:winntsystem32Ica PassThroughpn.exe”, 6, True
set wshshell = nothing

Of course, this code would BOMB!  The script would return an error complaining it couldn’t find the file.    Thanks to quick posting in the news:// newsgroup. 

Al Dunbar, world famous fellow MVP posted the answer to my prayers!

Your .run command is trying to run something called “C:winntsystem32ica” and pass it a parameter called “PassThroughpn.exe”. This is the same thing you would get if you typed the following at a command prompt:

    c:winntsystem32Ica PassThroughpn.exe

If the name of the file to run is actually “c:winntsystem32Ica PassThroughpn.exe”, you would enter it at the command prompt as:

    “c:winntsystem32Ica PassThroughpn.exe”

The double quotes in your code do not form part of the filename string being passed to the .run method, they are required to indicate a literal string.
You can prove this is the case by changing your script to look like this:

> Set wshShell = WScript.CreateObject (“”)
> c:winntsystem32Ica PassThroughpn.exe, 6, True
> set wshshell = nothing

Which will throw a syntax error (for rather obvious reasons). You need to pass a string that actually contains the quoted filename, which can be done this way:

> “””c:winntsystem32Ica PassThroughpn.exe”””, 6, True

Within a literal string, a single double-quote character is represented by two double-quote characters.   Notice the Three double quotes around the string,  This worked!  Thought I’d pass this tip along.

If you had ONLY one website to read, what would you choose?

I was at lunch,  we were talking about one “thing” would you chose?  Items ranged from if you were on a desert island, which food would you have?  Pizza was king!   One person asked, if you had ONLY one website to read, what would it be?  That was a mind bender actually!  Whats your vote?  Mine is an obvious one!

Connect to a remote server with different user/password using .NET/WMI

I’ve seen various posts in the newsgroups how to use .NET to connect to a remote server to collect WMI information. 

Dim options As New ConnectionOptions()
options.Username = “DomainUserId”
options.Password = “password”

Dim scope As New ManagementScope(“\ServerNamerootcimv2”, options)
Dim strSVCquery As String = ConfigurationSettings.AppSettings(“NICquery”)

Dim objNICQuery As New WqlObjectQuery(strSVCquery)
Dim objNICsearcher As New ManagementObjectSearcher(scope, objNICQuery)

Dim envVar As New ManagementObject()

Dim objNICItem As PropertyData
Dim strNICColName As String

For Each envVar In objNICsearcher.Get
        For Each objNICItem In envVar.Properties
            strNICColName = objNICItem.Name
            If Not IsArray(objNICItem.Value) Then
                Console.WriteLine(“Item is NOT an array — ” & strNICColName)
                Console.WriteLine(“Item is an array — ” & strNICColName)
            End If


A Web Monitor Server

This is my own project that I recently did to learn how to write Windows Services using .NET.  This is drop-dead simple using the framework.  Current .25 release # This project is called AWEBMONITORSERVICE. This is written in C# that is designed currently to monitor a single website. This service will email, write to the Event Log or write to a SQL database.

Download source!

Windows 2003 Active Directory anyone!

I spent Sunday afternoon (yes it was 10 degrees where i’m at), reading an ebook from netpro.   This is a six chapter series on Active Directory Troubleshooting.  Good read for those who are doing server admin with Active Directory.  This ebook targets windows 2000 active directory but apply’s to Windows 2003 as well.  My favorite chapter was #4, this chapter covers performance counters to use to help tune and monitor AD along with server counters.  is the homepage  These are free however you have to register.    I’d highly recommend anyone to download this good resource. 

Secondly, download the Windows 2003 server resource kit.  The resource kit is rich in utilities, one of my favorite is called getmac, this gets the MAC address of remote machines, you can even pass credentials.  has more info also (IIS 6 resource kit!).

Lastly this is my favorite geek toy of the month!  Microsoft released Services for Unix 3.5 recently w/o an license restrictions or cost.  Download today at