vbs脚本实现将OU中用户同步到用户组或通讯组
Dim OU(5)
Dim GROUP(5)
OU(1)=("OU=上海分公司,DC=test,DC=com")
GROUP(1)=("CN=上海分公司,OU=HAB,DC=test,DC=com")
OU(2)=("OU=厦门分公司,DC=test,DC=com")
GROUP(2)=("CN=厦门分公司,OU=HAB,DC=test,DC=com")
OU(3)=("OU=产品管理中心,OU=总公司,DC=test,DC=com")
GROUP(3)=("CN=产品管理中心,OU=HAB,DC=test,DC=com")
OU(4)=("OU=系统维护部,OU=产品管理中心,OU=总公司,DC=test,DC=com")
GROUP(4)=("CN=系统维护部,OU=HAB,DC=test,DC=com")
OU(5)=("OU=产品管理中心一部,OU=产品管理中心,OU=总公司,DC=test,DC=com")
GROUP(5)=("CN=产品管理中心一部,OU=HAB,DC=test,DC=com")
for i= 1 to UBound(OU)
CSVDE(OU(i))
'MsgBox "user.txt导出完毕,开始执行将OU"&OU(i)&"中有,通讯组"&GROUP(i)&"中没有的用户添加进通讯组"&GROUP(i)
OUTG(GROUP(i))
'MsgBox "开始执行将GROUP"&GROUP(i)&"中有,OU"&OU(i)&"中没有的用户从OU"&OU(i)&"中删除"
GTUser(GROUP(i))
'MsgBox "开始删除C:\user.txt"
Dele
'MsgBox"done"
Next
MsgBox "执行完毕"
Function CSVDE(OU)
Set oFSO=CreateObject("scripting.filesystemobject")
Set ws=CreateObject("WScript.Shell")
ws.run("csvde -f C:\user.txt -p onelevel -r objectclass=user -l dn -d "&OU)
For l=1 To 1000
if oFSO.FileExists("C:\user.txt")=0 Then
WScript.Sleep 5
else l=1000
End If
Next
End Function
'添加OU里有但通讯组里没有的user
Function OUTG(GROUP)
'On error resume Next
Const ForReading= 1
Const ADS_PROPERTY_APPEND=3
Set oFSO= CreateObject("scripting.filesystemobject")
Set oTF= oFSO.OpenTextFile("C:\user.txt",ForReading,True)
Set objGroup= GetObject("LDAP://"&GROUP)
a=objGroup.member
If (IsEmpty(a)<>True) Then arrMemberOf=objGroup.GetEx("member") 'MsgBox objgroup.displayname&"成员个数为"&UBound(arrMemberOf)+1 For k=0 To UBound(arrMemberOf) st=Chr(34)&arrMemberOf(k)&Chr(34)&Chr(13)&Chr(10)& st Next 'MsgBox "222"&objgroup.displayname&st Do While oTF.AtEndOfStream<>True sLine=oTF.ReadLine If sLine<>"DN,(null)" Then 'MsgBox "sLINE="&sLine If InStr(st,sLine) Then ' MsgBox sLine&"已经在通讯组"&objGroup.displayname&"中了" Else ' MsgBox sLine&"不在通讯组"&objGroup.displayname&"中"&Chr(13)&Chr(10)&st aline=split(sline,"",-1,1) Namel=Trim(aLine(0)) Name2=left(Namel,len(Namel)-1) Name3=right(Name2,len(Name2)-1) ' MsgBox Name3 objGroup.PutEx ADS_PROPERTY_APPEND,"member",Array(Name3) objGroup.Setinfo End If Else End If LoopElse'MsgBox "objGroup.member为空="&objGroup.member&"a="&aDo While oTF.AtEndOfStream<>True sLine=oTF.ReadLine If sLine<>"DN,(null)" Then 'MsgBox "sLINE="&sLine aline=split(sline,"",-1,1) Namel=Trim(aLine(0)) Name2=left(Namel,len(Namel)-1) Name3=right(Name2,len(Name2)-1) 'MsgBox Name3 objGroup.PutEx ADS_PROPERTY_APPEND,"member",Array(Name3) objGroup.Setinfo Else End If LoopEnd If
End Function
'删除OU里没有但通讯组里有的user
Function GTUser(GROUP)
'On error resume Next
Const ForReading=1
Const ADS_PROPERTY_DELETE=4
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile("C:\user.txt",ForReading)
If objFile.AtEndOfStream=True Then strContents=0 'MsgBox "user.txt是空的strContent="&strContents Else 'MsgBox "user.txt不是空的strContent="&strContents strContents=objFile.ReadAll objFile.CloseEnd If
Set objGroup=GetObject("LDAP://"&GROUP)
objGroup.Getinfo
'MsgBox "111"&objGroup.displayName
a=objGroup.member
If IsEmpty (a)<>True Then arrMemberOf=objGroup.GetEx("member") 'MsgBox "成员个数为"&UBound(arrMemberOf)+1 For k=0 To UBound(arrMemberOf) st=arrMemberOf(k) &Chr(13)&Chr(10)& st Next 'MsgBox st For Each strMember in arrMemberOf If InStr(strMember,"HAB") Then 'MsgBox strMember&"包含HAB" Else If InStr(strContents,strMember) Then Else objGroup.PutEx ADS_PROPERTY_DELETE,"member",Array(strMember) objGroup.SetInfo End if End If NextElse 'MsgBox "objGroup.member空,不要用执行删除"End If
End Function
Function Dele
Set oFSO=CreateObject("scripting.filesystemobject")
ofso.deletefile"C:\user.txt"
End Function