Reading a User's OCS Properties with a Perl Script

OCS script to retrieve a user's Office Communication Server properties: A visitor just requested this script.  Great idea.  Here's a Perl script that retrieves all of a user's OCS properties. You can also find the PowerShell version of this code in the article - Read OCS User Properties with PowerShell.

The script uses ADODB to search the Active Directory for the user, retrieves each of the OCS related attributes, and prints them to the screen.  It's a handy way to determine if the user is OCS enabled, what their SIP address is, what policies are set, whether the user's IM's are archived, etc.

The script demonstrates how to read each of these properties, including the option flags, which is a bit mask of various options, as well as the policies, which are DNwithBinary objects that require a little skill to unpack.

To use the script, you have to have Perl of course, which can be downloaded free from ActiveState. Next, copy the code and save it as a perl script file, such as getOCSproperties.pl then, from the command prompt, launch the script, using the user name (common name, logon name, or display name) as the argument.  Use quotes around the user name if there are any spaces in it.  Examples are:

getOCSproperties.pl myUser
getOCSproperties.pl "Seltzer, Brian"

Here's the script:
use Win32::OLE;
use XML::Simple;
 
$user=$ARGV[0];
$user=~s/\"//g;
 
# setup ADODB search parameters
$dse=Win32::OLE->GetObject("LDAP://RootDSE");
$root=$dse->Get("RootDomainNamingContext");
$adpath="GC://$root";
$base="<".$adpath.">";
$c = Win32::OLE->new("ADODB.Connection");
$c->{Provider} = "ADsDSOObject";
$c->Open("ADSI Provider");
$comm=Win32::OLE->new("ADODB.Command");
$comm->{ActiveConnection}=$c;
$comm->{Properties}->{'Page Size'}=1000;
$rs = Win32::OLE->new("ADODB.RecordSet");
 
$comm->{CommandText}="$base;(¦(cn=$user)(samaccountname=$user)(displayName=$user));distinguishedName;subtree";
$rs=$comm->Execute;
if($rs->{recordCount} < 1){
 print "$user not found\n";
}
 
until ($rs->EOF){
 $dn=$rs->Fields(0)->{Value};
 if($userobj=Win32::OLE->GetObject("LDAP://$dn")){
 
  print "\nOCS Properties for $userobj->{cn} ($userobj->{displayName}):\n\n";
 
  #is the user enabled?
  $enabled=$userobj->Get("msRTCSIP-UserEnabled");
  if($enabled == 1){ $enabled = "True"; }else{ $enabled = "False"; }
  print "\t     Enabled for OCS:\t$enabled\n";
 
  if($enabled eq "True"){
   print "\t    User SIP Address:\t".$userobj->Get("msRTCSIP-PrimaryUserAddress")."\n";
   print "\tUser OCS Pool/Server:\t".(split /,CN=/,$userobj->Get("msRTCSIP-PrimaryHomeServer"))[2]."\n";
 
   #policies
   $pol=$userobj->Get("msRTCSIP-UserPolicy");
   if(ref($pol) eq "ARRAY"){
    foreach $p (@{$pol}){
     &readPolicy($p);
    }
   }else{
    &readPolicy($pol);
   }
 
   #archiving
   $archive=$userobj->Get("msRTCSIP-ArchivingEnabled");
   if($archive == 0){ $archive="False"; }else{ $archive = "True"; }
   print "\t   Archiving Enabled:\t$archive\n";
 
   #federation
   $federation=$userobj->Get("msRTCSIP-FederationEnabled");
   if($federation == 0){ $federation = "False"; }else{ $federation = "True"; }
   print "\t  Federation Enabled:\t$federation\n";
 
   #option Flags
   $flags=$userobj->Get("msRTCSIP-OptionFlags");
   if(($flags & 1) == 1){ $pim = "True"; }else{ $pim = "False"; }
   if(($flags & 16) == 16){ $rcc = "True"; }else{ $rcc = "False"; }
   if(($flags & 64) == 64){ $oam = "True"; }else{ $oam = "False"; }
   if(($flags & 128) == 128){ $uce = "True"; }else{ $uce = "False"; }
   if(($flags & 256) == 256){ $ep = "True"; }else{ $ep = "False"; }
   if(($flags & 512) == 512){ $rcd = "True"; }else{ $rcd = "False"; }
   if(($flags & 1024) == 1024){ $aa = "True"; }else{ $aa = "False"; }
 
   print "\n\t        Option Flags:\t$flags\n";
   print "\t    Public IM Enabed:\t$pim\n";
   print "\t Remote Call Control:\t$rcc\n";
   print "\t  Anonymous Meetings:\t$oam\n";
   print "\t       Unified Comms:\t$uce\n";
   print "\t   Enhanced Presence:\t$ep\n";
   print "\t  Rmt Call Dual CTRL:\t$rcd\n";
   print "\t      Auto Attendant:\t$aa\n";
  }
 }else{
  print "$user access denied\n";
 }
 $rs->MoveNext;
}
 
sub readPolicy{
 $pol=shift;
 if($pol){
  $bin=$pol->{BinaryValue};
  @byteArray=unpack("C*",$bin);
  $binString="";
  foreach $byte (@byteArray){
   $binString=$binString.sprintf("%02x",$byte);
  }
  $poltype="";
  if($binString eq "01000000"){  $poltype=" Meeting Policy"; }
  if($binString eq "02000000"){  $poltype="   Voice Policy"; }
  if($binString eq "04000000"){  $poltype="Presence Policy"; }
  $polObj=Win32::OLE->GetObject("LDAP://$pol->{DNString}");
  $policyContent=$polObj->Get("msRTCSIP-PolicyContent");
  $hashref=XMLin($policyContent);
  $polname = $hashref->{property}->{Name}->{content};
  print "\t     $poltype:\t$polname\n";
 }
}

Enjoy!  We appreciate the visitor's request.  It's good to know what people are looking for, and we're happy to help when we can.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...