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  
    Loop
Else

'MsgBox "objGroup.member爲空="&objGroup.member&"a="&a

Do 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  
    Loop

End 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.Close
End 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

        Next

Else
    'MsgBox "objGroup.member空,不要用執行刪除"
End If      

End Function

Function Dele
Set oFSO=CreateObject("scripting.filesystemobject")
ofso.deletefile"C:\user.txt"
End Function

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章