One of the OCS projects I’m working on is building out a provisioning system for both Exchange UM and OCS voice for their upcoming OCS voice pilot. 

If you’ve ever dealt with OCS automation you know its all WMI-based.  WMI isn’t the most friendly way of doing this stuff, and if you’re a scripter or a programmer, it certainly requires some brain wrapping to understand how it all works.  Worse yet, there’s not a lot of documentation (that I was able to find – perhaps I didn’t look in the right place) on how to manage OCS users programmatically using any language, let alone C# and .NET.

I’ve mostly finished up the OCS module, and wanted to share the C# and PowerShell code that will help you programmatically enable your users for OCS voice.  This code segment is certainly part of a larger application, but you’ll be able to get the jist of it.  You’ll need to make sure that you retrieve the LDAP distinguishedName values for your specified Location Profile and Voice Policy and assign those to targetLocationProfileDN and targetVoicePolicyDN respectively.  You could use System.DirectoryServices to look it up on the fly, or use a configuration file and specify it there and just bring that in.

C# Example:

ManagementObject userOCSAccount = null;

          string targetLocationProfileDN = "CN={6D7A2437-N0TA-R3AL-GU1D-F7B464597478},CN=Location Profiles,CN=RTC Service,CN=Services,CN=Configuration,DC=contoso,DC=com";
          string targetVoicePolicyDN = "CN={A78D1ACE-N0TA-R3AL-GU1D-FEA023942FEC},CN=Policies,CN=RTC Service,CN=Services,CN=Configuration,DC=contoso,DC=com";

          //  Build the WMI query that we'll use to look up the user's information
          SelectQuery userWMIQuery = new SelectQuery( "SELECT * FROM MSFT_SIPESUserSetting WHERE PrimaryURI=’sip:someone@contoso.com’" );

            using( ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher( userWMIQuery ) )
            {
                foreach( ManagementObject searchResults in wmiSearcher.Get() )
                {
                    userOCSAccount = searchResults;
                }
            }

            userOCSAccount[ "LineURI" ] = "tel:+14257064020";
            userOCSAccount[ "LocationProfile" ] = targetLocationProfileDN;
            userOCSAccount[ "UCPolicy" ] = targetVoicePolicyDN;
            userOCSAccount[ "UCEnabled" ] = false; 

            //  Let’s save the changes back to OCS
            userOCSAccount.Put();
            userOCSAccount.Dispose();

PowerShell Example:

$targetLocationProfileDN = “CN={6D7A2437-N0TA-R3AL-GU1D-F7B464597478},CN=Location Profiles,CN=RTC Service,CN=Services,CN=Configuration,DC=contoso,DC=com”

$targetVoicePolicyDN = “CN={A78D1ACE-N0TA-R3AL-GU1D-FEA023942FEC},CN=Policies,CN=RTC Service,CN=Services,CN=Configuration,DC=contoso,DC=com”

$userOCSAccount = Get-WmiObject –Query “SELECT * FROM MSFT_SIPESUserSetting WHERE PrimaryURI=’sip:someone@contoso.com’”
if( $userOCSAccount –ne $null )
{
      $userOCSAccount.LineURI = “tel:+14257064020”
      $userOCSAccount.LocationProfile = $targetLocationProfileDN
      $userOCSAccount.UCPolicy = $targetVoicePolicyDN
      $userOCSAccount.UCEnabled = $true

      $userOCSAccount.Put()
}

If you’re diving into the world of OCS automation either through scripting, PowerShell, or .NET, I hope these snippets get you pointed in the right direction.  I’ll have more code snippets as I go, and I’ll post them here.  Good luck!