You are on page 1of 2

'***************************************************

'This script will pull all users that have Dial-in


'access from Active Directory and the OU the account
'is in and writes the values out to a CSV file
'***************************************************
Option Explicit

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Dim objConnection, objCommand, objRootDSE


Dim objRecordSet, ou
Dim namingContext, fso, outFile

Set objConnection = CreateObject("ADODB.Connection")


Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

set objRootDSE = getobject("LDAP://RootDSE")


namingContext = objRootDSE.Get("defaultNamingContext")
set objRootDSE = nothing

Set fso = CreateObject("Scripting.FileSystemObject")


Set outFile = fso.CreateTextFile("RRAS_VPN_Users.txt", True)

objCommand.Properties("Page Size") = 1000


objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
"SELECT Name FROM 'LDAP://" & namingContext & _
"' WHERE objectCategory='user' " & _
"AND msNPAllowDialin = TRUE"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
' Call function to find OU from computer name
ou = getOUByUserName(objRecordSet.Fields("Name").Value)
outFile.WriteLine(objRecordSet.Fields("Name").Value & _
",'" & ou & "'")
objRecordSet.MoveNext
Loop

outFile.Close

WScript.Echo "Complete"

WScript.Quit

function getOUByUserName(byval UserName)


' *** Function to find ou/container of user object ***

DIM namingContext, ldapFilter, ou


DIM cn, cmd, rs
DIM objRootDSE

set objRootDSE = getobject("LDAP://RootDSE")


namingContext = objRootDSE.Get("defaultNamingContext")
set objRootDSE = nothing

ldapFilter = "<LDAP://" & namingContext & _


">;(&(objectCategory=User)(name=" & userName & "))" & _
";distinguishedName;subtree"

set cn = createobject("ADODB.Connection")
set cmd = createobject("ADODB.Command")

cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
cmd.commandtext = ldapFilter

set rs = cmd.execute

if rs.eof <> true and rs.bof <> true then


ou = rs(0)
ou = mid(ou,instr(ou,",")+1,len(ou)-instr(ou,","))
getOUByuserName = ou

end if

rs.close
cn.close

end function

You might also like