以下代码导入csv文件数据到AD。导入用户的电话号码
'=============================================================
'
' CSV文件格式:
' userPrincipalName,mobile
' 例如: test@console.com, 13455667788
'
'=============================================================
Dim sComputer,sDomain, sLoginName, sMobile
sComputer = "LDAP://192.168.0.1/"
sDomain = "DC=console,DC=csg"
sPath = "d:\test.csv"
sLocation = "ou=FromMIIS"
set objArgs = wscript.arguments
'If ObjArgs.Count < 1 Then
' WScript.Echo "Please tell me your input file name! "
' WScript.Quit (0)
'end if
Set objFSO = CreateObject("Scripting.FileSystemObject")
If not objFSO.FileExists(sPath) Then
WScript.Echo "Cannot open input file: " & ObjArgs(0)
WScript.Quit (1)
End If
'msgbox "1"
sFile=sPath
sLogFile="c:\Tools\Test"+"-RunLog.txt"
sPasswdFile="c:\Tools\Test"+"-PasswordFile.txt"
'msgbox "2"
Set fso=CreateObject("Scripting.FileSystemObject")
Set fread=fso.OpenTextFile(sFile)
'msgbox "3"
sFailLog=vbnewline+vbnewline+"创建失败的用户"+vbNewline+vbNewline
sSuccessLog=vbnewline+vbnewline+"成功创建的用户"+vbNewline+vbNewline
sRunLog=vbnewline+vbnewline+"用户导入过程开始时间 "+Cstr(date)+" "+Cstr(time)+vbNewLine+vbNewline
sPasswordLog=vbnewline+vbnewline+"口令设置过程开始时间 "+Cstr(date)+" "+Cstr(time)+vbNewLine+vbNewline
Do While fread.AtEndOfStream<>True
sLine1=Trim(Replace(fread.ReadLine," ",""))
sTempA=Split(sLine1, ",")
'----------------------------------------------------------------------
' The splited Array should contain the following info by sequence:
'
' fullName,userid,employeeNumber,company,branch,department
'
'---------------------------------------------------------------------
if (LCase(sTempA(0))="null") then
sRunLog=sRunLog+"Invalid user ID: "+sLine1+vbNewline
sError=1
Else
sLoginName = sTempA(0)
end if
if (LCase(sTempA(1))="null") then
sRunLog=sRunLog+"Invalid user mobile: "+sLine1+vbNewline
sError=1
Else
sMobile= sTempA(1)
end if
' msgbox sLine
If (sError=0) Then
FindCreateOU sLoginName, sMobile
End If
sError=0
Loop
'Set fWrite=fso.OpenTextFile(sLogFile,8,true)
'Set fPWD=fso.OpenTextFile(sPasswdFile,8,true)
'fwrite.WriteLine sRunLog
'fwrite.WriteLine sFailLog
'fwrite.WriteLine sSuccessLog
'fPWD.WriteLine sPasswordLog
'fwrite.close
'fPWD.close
'fread.close
wscript.echo "Done - Please check log file for possible errors"
'***********************************************************************************
'* Following are functions
'***********************************************************************************
Function FindCreateOU(sDN, sTel)
'msgbox 11
strDN = "<"&sComputer & sDomain &">;(&(objectClass=User)(userPrincipalName=" & sDN &"));adspath;subtree"
'Set objUser = GetObject(strDN)
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = strDN
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
While Not objRecordSet.EOF
path = objRecordSet.Fields(0).value
Set objUser = GetObject(path)
objUser.Put "mobile", sTel
objUser.SetInfo
objRecordSet.MoveNext
Wend
End Function