Enable an OCS User using VB.NET

OCS Script: Here's how to enable an Office Communication Server (OCS) user in Active Directory using Visual Basic and the .Net Framework. You can find a VBScript version of this code here: Enable an OCS User using VBScript and a Perl version here: Enable an OCS User using Perl

In this code, I use the System.DirectoryServices namespace of the .Net Class Library. I use the DirectorySearcher to find the user, then I populate the OCS related properties to configure and enable the user for OCS.

To make this code work, you need .Net Framework version 2.0 or higher. In Visual Studio, create a Visual Basic console application. After the solution is open, right-click the project, click "Add Reference", and on the .Net tab, select "System.DirectoryServices" and click OK. Then, past the source code into the project.

Finally, put in a valid user name, and fix the domain names in the entry, meetingPolicy, and homeServerDN values. Compile and test.

Imports System.DirectoryServices
Module Module1
Dim entry As New DirectoryEntry("GC://dc=myDomain,dc=com")
    Dim searcher As New DirectorySearcher(entry)
    Dim results As SearchResultCollection
    Dim meetingPolicy As String = "CN={BA9AD4FA-3516-47BD-A2C2-4556AE4D7263},CN=Policies,CN=RTC Service,CN=Services,CN=Configuration,DC=myDomain,DC=com"
    Dim meetingPolicyDNwithBinary As String = "B:8:01000000:" & meetingPolicy
    Dim homeServerDN As String = "CN=LC Services,CN=Microsoft,CN=OCSPOOL01,CN=Pools,CN=RTC Service,CN=Services,CN=Configuration,DC=myDomain,DC=com"
Sub Main()
        Dim myUser As String = "kzzz111"
        Try
searcher.Filter = "(&(objectCategory=user)((cn=" & myUser & ")(samaccountname=" & myUser & ")))"
            searcher.SizeLimit = 100
            results = searcher.FindAll
            If results.Count < 1 Then
                Console.WriteLine("User " & myUser & " was not found")
            End If
            For Each result As SearchResult In results
                Dim dn As String = result.Properties("distinguishedName").Item(0)
                Dim userEntry As New DirectoryEntry("LDAP://" & dn)
                Dim mail As String = userEntry.Properties("mail").Item(0)
                writeProperty(userEntry, "msRTCSIP-ArchivingEnabled", 0)
                writeProperty(userEntry, "msRTCSIP-FederationEnabled", True)
                writeProperty(userEntry, "msRTCSIP-OptionFlags", 256)
                writeProperty(userEntry, "msRTCSIP-PrimaryHomeServer", homeServerDN)
                writeProperty(userEntry, "msRTCSIP-PrimaryUserAddress", "sip:" & mail)
                writeProperty(userEntry, "msRTCSIP-UserEnabled", True)
                writeProperty(userEntry, "msRTCSIP-UserPolicy", meetingPolicyDNwithBinary)
                userEntry.CommitChanges()
                Console.WriteLine(myUser & " is now enabled for OCS")
            Next
Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try
    End Sub
Sub writeProperty(ByVal userEntry As DirectoryEntry, ByVal propName As String, ByVal propValue As Object)
        If userEntry.Properties(propName).Count = 0 Then
            userEntry.Properties(propName).Add(propValue)
        Else
            userEntry.Properties(propName).Item(0) = propValue
        End If
    End Sub
End Module

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...