OUClass

Now it's time to build a class to represent the OU object and its capabilities. Be aware that not all the functionality you have for an OU in the graphical administration tools is available to you as a programmer. One of the most requested features in the newsgroups—the ability to create GPOs— is not yet available to programmers. However, the ability to link GPOs to organizational units is within our grasp.

Option Explicit Class OUClass

' Private and public variables Private m_OUObject

Private Sub C1ass_Initia1ize End Sub

Private Sub C1ass_Terminate End Sub

Public Function BindToOU(OUPath) If ADObjectExists(OUPath) Then

Set m_OUObject = GetObject(FormatAdsPath(OUPath)) m_OUObject.GetInfo Set BindToOU = m_OUObject Else

Err.Raise 102,m_ErrSource,"Object does not exist" End If

End Function

Public Sub Create(ContainerPath,CN) If CN = "" Or ContainerPath = "" Then

Err.Raise 101,m_ErrSource,"Missing arguments" Exit Sub End If

Dim Container

If ADObjectExists(ContainerPath) Then

Set Container = GetObject(FormatAdsPath(ContainerPath)) Set m_OUObject = Container.Create("organizationa1Unit",FormatCN(CN)) m_OUObject.SetInfo m_OUObject.GetInfo Set Create = m_OUObject Else

WScript.Echo "Error connecting to container!" WScript.Quit(l) End If End Sub

Public Sub Delete m_OUObject.De1eteObject(0) End Sub

Public Sub Move(Destination) Dim NewContainer

Set NewContainer = GetObject(FormatAdsPath(Destination)) NewContainer.MoveHere m_OUObject.AdsPath, m_OUObject.Name m_OUObject.MoveHere FormatAdsPath(Destination),FormatCN(Destination) End Sub

Public Property Get ChildCount m_OUObject.GetInfoEx Array("msDS-Approx-Immed-Subordinates"),0 ChildCount = m_OUObject.Get("msDS-Approx-Immed-Subordinates") End Property

Public Property Get ChildObjects ' This returns a Dictionary object! Dim Child

Set ChildObjects = WScript.CreateObject("Scripting.Dictionary") For Each Child in m_OUObject

ChildObjects.Add Child.Name, Child.Class Next

End Property

Public Sub LinkGPO(GPOName)

Dim SearchRoot,FilterText,Attributes,SearchScope,GPOPath Dim RootDSE

Set RootDSE = "LDAP://RootDSE"

SearchRoot = "<LDAP://cn=policies,cn=system," & _

RootDSE.Get("defaultNamingContext") & ">;" Set RootDSE = Nothing

FilterText = _

"(&(objectcategory=grouppo1icycontainer)" & _ "(objectc1ass=grouppo1icycontainer)" & _ "(disp1ayname=" & GPOName & "));" Attributes = "AdsPath" SearchScope = "OneLevel" Dim Cn, Cmd, Results

Set Cn = CreateObject("ADODB.Connection") Set Cmd = CreateObject("ADODB.Command") Cmd.ActiveConnection = Cn

Cmd.CommandText = SearchRoot & ";" & FilterText & ";" & _ Attributes & ";" & SearchScope Set Results = Cmd.Execute Select Case Results.RecordCount Case 0

WScript.Echo "No GPO found named " & GPOName WScript.Quit(l) Case 1

GPOPath = Results.Fields(O) Case Else

WScript.Echo "More than 1 matching GPO found!" WScript.Quit(l) End Select

Set Results = Nothing Set Cmd = Nothing Set Cn = Nothing On Error Resume Next Dim CurrentGPOLinks

CurrentGPOLinks = m_OUObject.Get("gpLink") If Err.Number > 0 Then

WScript.Echo "Error retrieving current GPO Links" End If

On Error GoTo 0

m_OUObject.Put "gpLink",CurrentGPOLinks & "[" & GPOPath & ";0]" End Sub

Private Function ADObjectExists(ADsPath)

Dim ADObject, LDAPObject

ADSPath = FormatAdsPath(AdsPath)

On Error Resume Next

Set LDAPObject = GetObject("LDAP:")

LDAPObject.OpenDSObject(ADsPath,vbNu11String,vbNu11String,

ADS_FAST_BIND)

If Err.Number = 0 Then

ADObjectExists = True

Else

ADObjectExists = False End If

Set ADObject = Nothing Set LDAPObject = Nothing End Function

Private Function FormatAdsPath(ADsPath) If CBoo1(Instr(ADsPath,"LDAP://")) Then

FormatAdsPath = AdsPath Else

FormatAdsPath = "LDAP://" & ADsPath End If

End Function

Public Property Get m_ErrSource m_ErrSource = "OUClass" End Property

Public Property Get ADS_FAST_BIND

ADS_FAST_BIND = &H20 End Property

End Class

Was this article helpful?

0 0
Computer Hard Drive Data Recovery

Computer Hard Drive Data Recovery

Learn How To Recover Your Hard Drive Data After A Computer Failure.

Get My Free Ebook


Post a comment