Using PS Math to check a flag value – Checking Lync RCC without Lync PS Module

I had a unique situation where I didn’t want to deploy the Lync 2013 powershell module to a group of people, but I wanted a way to quickly detect if remote call control (rcc) was enabled for the user. I wasn’t concerned with modifying the setting and simply wanted to read the setting.

I found that the RCC settings for Lync are stored as flags in the AD object. You can view this using ADSI edit and looking for the property “msRTCSIP-OptionFlags”. Then, using this as a reference, we can decode if RCC is configured.

Powershell [math] to the rescue!

In order to tackle this, I needed to know that in behind the scenes the flag is actually calculated using binary. This means that each flag is tied to a power of 2. For example 2^5 = 32. So what we’ll do is find the highest power of two that is less than our flag value of 16. Then we’ll subtract it and do it again. We’ll stop as soon as we’re down to flags totaling less than 32 (1 power of 2 higher than the flag we want). If this number is less than 16, RCC is not enabled. If this number is greater than or equal to 16 RCC is enabled.

First, we’ll need a function to find the highest power of two for a number:

Function Get-HighestPowerOf2{
    #Returns the highest x in 2^x -le $number
    Param(
        [double]$number
    )
    return ([math]::Floor([math]::log($number,2)))
}

Second, we’ll need a function to use the highest power of two and subtract it until we get down to a number less than 32 and check if it is greater than or equal to 16.

Function Get-FlagEnabled{
    Param(
        [double]$Flags,
        [double]$DesiredFlag
    )
    #Get one higher power of two than the flag we want.
    $intOneHigherFlag = [math]::pow(2, (Get-HighestPowerOf2 -number $DesiredFlag) + 1)

    #Remove the sum of all the powers of two higher than the flag we want until the value is less than the flag above the one we want
    Do{
        $Flags = $Flags - ([Math]::pow(2,(Get-HighestPowerOf2 -number $Flags)))
    }While($Flags -ge $intOneHigherFlag) #one power of two higher than 16 (2^5)

    #If the remaining flags are greater than or equal to our desired flag, then our flag is enabled otherwise it is disabled
    If($Flags -ge $DesiredFlag){
        Return $true
    }else{
        Return $false
    }
}

Then test!

#Get the AD user
$objADUser = Get-ADUser -Properties msRTCSIP-OptionFlags -filter {SamAccountName -eq "YourUserName"}

#Get the flags property
$intFlags = [Double]($objADUser | Select-Object -ExpandProperty "msRTCSIP-OptionFlags")

#Is RCC enabled?
Get-FlagEnabled -Flags $intFlags -DesiredFlag 16

Note that this should work for any field that is stored as a flag. You just need to know what flag value you want.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s