I have written a scrip that will query our Active Directory for users in a specific domain\group and now I need to be able to connect to our Global Directory to get thier Employee Number and Transit Number(custom field I'm told). I'm not too sure what I need to do to get this working.
Code:
On Error Resume Next
set WshShell = CreateObject("WScript.Shell")
set FSO = CreateObject("Scripting.FileSystemObject")
Set objDOM = CreateObject("Microsoft.XMLDOM")
' Connection to AD **************************************
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
' *******************************************************
ProfileXML = "\\s3w01904\g$\bisk\Stats\Profiles.xml"
TotalsFile = "\\s3w01904\g$\bisk\Stats\totals.txt"
DetailsFile = "\\s3w01904\g$\bisk\Stats\details.txt"
excelDetails = "\\s3w01904\g$\bisk\Stats\Details.csv"
excelPath = "\\s3w01904\g$\bisk\Stats\Users.csv"
Const Increment = 1500
LowRange = 0
HighRange = int(increment) - 1
DeleteFile (excelPath)
DeleteFile (excelDetails)
DeleteFile (TotalsFile)
DeleteFile (DetailsFile)
Set objExcelFile = FSO.CreateTextFile(excelPath)
Set objExcelFileDetails = FSO.CreateTextFile(excelDetails)
objExcelFile.WriteLine "SCENARIO,FISCAL_PERIOD,COST_CENTRE,SERV_CODE,OPT_CODE,CNSIND,VOL,EMPNUM,
REMARKS,USER_NAME"
objExcelFileDetails.WriteLine "Monthly Cost,Yearly Cost,Details"
If FSO.FileExists (ProfileXML) Then
objDom.ValidateOnParse = True
objDom.Load(ProfileXML)
For Each ProfileNode in objDom.GetElementsByTagName("Stats/Profile")
ProfileName = ProfileNode.GetAttribute("Name")
MonthlyCost = ProfileNode.GetAttribute("Cost")
AddTextToFile DetailsFile , "***********************************************************"
AddTextToFile DetailsFile , "Profile - " & ProfileName
TotalUsers = 0
For Each GroupNode in ProfileNode.GetElementsByTagName("Group")
GroupName = GroupNode.GetAttribute("Name")
If GroupName = "WTS RBCI - Liberty" then
GroupName = "WTS RBCI – Liberty"
End If
Domain = GroupNode.GetAttribute("Domain")
bDone = False
UserCount = 0
LowRange = 0
HighRange = int(increment) - 1
If UCASE(Domain) = "PLATINUM" Then
'For NT4 Domain
Set objGroup = GetObject("WinNT://" & Domain & "/" & GroupName)
Set MemberList = objGroup.Members
For Each Member In MemberList
UserCount = int(UserCount) + 1
CurrentRow = CurrentRow + 1
objExcelFile.WriteLine ",,Transit Number,,,,,Emp Number," & ProfileName & "," & Member.Name
Next
Else
strDomainFQDN = "DC=" & Domain & ",DC=FG,DC=RBC,DC=COM"
objCommand.CommandText = _
"<GC://" & strDomainFQDN & ">;" & _
"(&(objectCategory=group)" & _
"(sAMAccountName=" & GroupName & "));" & _
"DistinguishedName;subtree"
Set objRecordSet = objCommand.Execute
If objRecordSet.RecordCount <> 0 Then
strDN = objRecordSet.Fields("DistinguishedName")
strDN = replace(strDN , "/" , "\/")
set objGroup = GetObject("LDAP://" & strDN)
While Not bDone
strRange = "member;range=" & lowRange & "-" & highRange
objGroup.GetInfoEx Array(strRange), 0
MemberList = objGroup.GetEx("member")
For Each Member in MemberList
If Member <> "" Then
UserCount = int(UserCount) + 1
set Objuser = GetObject("LDAP://" & Member)
objExcelFile.WriteLine ",,Transit Number,,,,," & objuser.employeeid & "," & ProfileName & "," & objuser.displayname
End If
Next
If int(UserCount) - 1 = HighRange Then
'still more users in the group
bDone = False
Else
'quits loop on short group or end of long group
bDone = True
End If
LowRange = LowRange + Increment
HighRange = HighRange + Increment
Wend
End If
End If
Set objRecordSet = nothing
Set obGroup = nothing
Set MemberList = nothing
Set objUser = nothing
AddTextToFile DetailsFile , Domain & "\" & GroupName & " - " & UserCount
TotalUsers = int(TotalUsers) + int(UserCount)
Next
AddTextToFile DetailsFile , "Total - " & TotalUsers
AddTextToFile TotalsFile , ProfileName & " - " & TotalUsers
TotalMonthlyCost = int(TotalUsers) * int(MonthlyCost)
TotalYearlyCost = 12 * int(TotalMonthlyCost)
objExcelFileDetails.WriteLine TotalMonthlyCost & "," & TotalYearlyCost & "," & ProfileName & " - " & TotalUsers & " users @ $" & MonthlyCost & " per month"
Next
AddTextToFile TotalsFile , "DONE!"
else
wscript.echo "XML doesn't exist"
end if
wscript.quit
'***************************************************************
Sub AddTextToFile(Filename, Text)
CONST FOR_APPENDING = 8
With FSO.OpenTextFile(Filename, FOR_APPENDING, True)
.WriteLine Text
End With
End Sub
Sub DeleteFile (Filename)
If FSO.FileExists (Filename) Then
FSO.DeleteFile Filename
End If
End Sub