MSADMIN.NET

Experiences in IT
posts - 20, comments - 139, trackbacks - 173

Checking If an AD attribute is NOT set.

OK, I just wanted to post this really quick for all of you vbscripters out there.

I was doing the following LDAP Search in my vbscript:

objCommand.CommandText = "<LDAP://" & strDomainNCDN & ">(&(sAMAccountName=" & strUserName & "));sAMAccountName,employeeID;subtree"

strDomainNCDN is just the Domain Naming Context Distinguished Name. That's what I call it anyway. It's actuall just the Distinguished Name of the Domain, but I get it by querying for the default naming context of whichever domain I'm working in like this:

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 500
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRootDSE = getObject("LDAP://rootDSE")
strDomainNCDN = objRootDSE.GET("DefaultNamingContext")
set objDomain = GetObject("LDAP://" & strDomainNCDN)

This keeps my scripts nice and Portable, which to me means there is no need to change the script to have it work in another environment.

So, back to my example above in bold. If I wanted to see if the employeeID is null I would do the following against the returned recordset:

If IsNull(objRecordset.Fields(1).Value) Then

   Do whatever

End If

 

So the key to checking for a null value in a recordset of Active Directory attributes returned from an LDAP query inside your vbscript is to check if ISNULL returns as TRUE.

So this also means that the following will check to see if the attribute is populated:

 

If IsNull(objRecordset.Fields(1).Value) = False Then

   Do whatever

End If

or

If NOT IsNull(objRecordset.Fields(1).Value) Then

   Do whatever

End If

 

This is because IsNull(objRecordset.Fields(1).Value)  checks the value of the field and either returns as TRUE if there is nothing there, or FALSE if there is something there.

That makes me wonder, why isn't there just built in function called IS, or DOESEXIST. That would be more natural. I mean, when you check for a file it's usually something like

If objectFileSystem.FileExists(PathToFileName) then

   Do Whatever

End If

 

But alas, we are stuck with ISNULL.

At least we have something...  even if it is nothing :-)

Print | posted on Thursday, December 27, 2007 2:24 PM

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: