Link to home
Create AccountLog in
Avatar of gmrstudios2013
gmrstudios2013

asked on

VB Script During Logon To Write Exchange CustomAttribute

Hello and thank you for reading.  I would like to run a VB Script during a users logon that pulls the IP address of the Host Machine they are on.  I would then like to take that IP address, matching the case of one of the octets, to set a defined value, and write it to the Exchange 2010's Custom Attribute1 (I believe it is extensionAttribute1 in AD) in Active Directory.

The goal of this would be do automatically assign users to Dynamic Distribution lists in Exchange 2010 based off the value set in Custom Attribute 1 from the VBScript.

My VBScripting is novice at best.  I think I am on the right track but I am stuck in debug land.

Option Explicit
Dim strUser, strComputer, rootDSE, adoConnection, ldapStr, adoRecord, objUser, strIP, thirdOctet

strUser = "username"
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootCIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 

For Each objItem In colItems
Set strIP = objItem.IPAddress(0)

Set rootDSE = GetObject("LDAP://RootDSE")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADSDSOObject"
adoConnection.Open "ADs Provider"

ldapStr = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">;(&(objectCategory=Person)(objectClass=User)(samAccountName=" & strUser & "));adspath;subtree"
Set adoRecord = adoConnection.Execute(ldapStr)
Set objUser = GetObject (adoRecord.Fields(0).Value)

thirdOctet = split(strIP, ".")
select case thirdOctet(2)  '3rd token/octet
   case "10"
		objUser.Put "extensionAttribute1", strUser & "Office1"
   case "20"
		objUser.Put "extensionAttribute1", strUser & "Office2"
   case "30"
		objUser.Put "extensionAttribute1", strUser & "Office3"
end select
objUser.SetInfo

Next

Open in new window


Thanks!
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gmrstudios2013
gmrstudios2013

ASKER

Rob,

Thank you for the quick response.

I am getting the following error when I run the script:

(5, 1) (null): 0x80041021

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootCIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 

For Each objItem In colItems
	Set strIP = objItem.IPAddress(0)
	thirdOctet = split(strIP, ".")
	select case thirdOctet(2)  '3rd token/octet
		case "10"
			objUser.Put "extensionAttribute1", "Office1"
		case "20"
			objUser.Put "extensionAttribute1", "Office2"
		case "30"
			objUser.Put "extensionAttribute1", "Office3"
	end Select
	objUser.SetInfo
Next

Open in new window

Ooops, didn't notice that.  Line 5 is missing a couple of backslashes, change it to:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\rootCIMV2") 

Open in new window


Regards,

Rob.
I am getting another error on the same line.

(5, 1) Microsoft VBScript runtime error: The remote server machine does not exist or is unavailable: 'GetObject'

Windows firewall is off on the machine I am running the script.  WMI services are working as well.

Does it have something to do with strComputer = "."?

When I do WScript.Echo "Computer: " _ & strComputer

It returns Computer: .
The error is because you have spaces around the dot for strComputer.

When you have
strComputer = "."

that means you are connecting to the local computer.  That is what you want from this script.

This should now be ammended.

Regards,

Rob.

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 

For Each objItem In colItems
	Set strIP = objItem.IPAddress(0)
	thirdOctet = split(strIP, ".")
	select case thirdOctet(2)  '3rd token/octet
		case "10"
			objUser.Put "extensionAttribute1", "Office1"
		case "20"
			objUser.Put "extensionAttribute1", "Office2"
		case "30"
			objUser.Put "extensionAttribute1", "Office3"
	end Select
	objUser.SetInfo
Next

Open in new window

Rob,

I am getting a new error when I run the script:

(9, 2) Microsoft VBScript runtime error: Obj
ect required: 'objItem.IPAddress(...)

Thanks again for all your help.
My apologies.  This is what happens when I don't test the code.  I have now tested it, and this should work for you.

Regards,

Rob.

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled='True'") 

For Each objItem In colItems
	If Not IsNull(objItem.IPAddress) Then 
        For intIPCount = LBound(objItem.IPAddress) To UBound(objItem.IPAddress)
        	If objItem.IPAddress(intIPCount) <> "0.0.0.0" Then
				strIP = objItem.IPAddress(intIPCount)
				Exit For
			End If
		Next
	End If
	thirdOctet = split(strIP, ".")
	select case thirdOctet(2)  '3rd token/octet
		case "10"
			objUser.Put "extensionAttribute1", "Office1"
		case "20"
			objUser.Put "extensionAttribute1", "Office2"
		case "30"
			objUser.Put "extensionAttribute1", "Office3"
	end Select
	objUser.SetInfo
Next

Open in new window

Fantastic, thank you for your help.  Was I at least in the ballpark on my initial script?
Yeah, pretty much.  You had this:
Set strIP = objItem.IPAddress(0)

which may have worked if you removed the Set keyword, and provided the NIC returned through Win32_NetworkAdapterConfiguration was the one you wanted to check.   I guess that's unlikely though, since most computers have multiple network adapters.  For that reason, you need to loop through all of the ip addresses by using
	If Not IsNull(objItem.IPAddress) Then 
        For intIPCount = LBound(objItem.IPAddress) To UBound(objItem.IPAddress)
        	If objItem.IPAddress(intIPCount) <> "0.0.0.0" Then

Open in new window


where the first line just makes sure an address is present for the NIC, the second loops through the available ip addresses for the system, and the third means you ignore 0.0.0.0 addresses.

Other than that, your script should have worked, but I also removed the LDAP search, since that's not required for local resource queries.  You can use the inbuilt ADSystemInfo object instead to bind to the local user or computer.

If you have any more questions, let me know.

Regards,

Rob.