vbs 导入csv文件数据到AD

 以下代码导入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



 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值