Home > OCS 2007 / Lync 2010, Powershell > OCS 2007 R2: Get number of PIC contacts with Powershell

OCS 2007 R2: Get number of PIC contacts with Powershell

**note** This script is not yet faultless

In this post I will publish one of my Powershell scripts I made to Count the number of PIC contacts per SIP URI contact within OCS 2007 R2. I accomplished this script with WMI because there is unfortunately no Powershell CMD-let available for OCS 2007 R2. The WMI classes MSFT_SIPESUserSetting and MSFT_SIPESUserContactData are required to retrieve all the user contact details. The count of PIC contacts are based on the supported PIC domains in KB897567.

The Powershell console output of the script:

This script is made in Powershell v1.0 and is only compatible with OCS 2007 R2.
Save the underneath script for example as “OCScount.ps1” and run it in the Powershell console.

#Version 0.1
#Created by Joris Thys
#More info: https://jthys.wordpress.com
#
#
#-------------------------------------------------
# Function: Count duplicate fields in array
#-------------------------------------------------
cls
Function Get-Duplicate {
 param($array, [switch]$count)
 begin {
 $hash = @{}
 }
 process {
 $array | %{ $hash[$_] = $hash[$_] + 1 }
 if($count) {
 $hash.GetEnumerator() | ?{$_.value -ge 1} | %{
 New-Object PSObject @{
 Contact = $_.key.ToString().substring(4)+"`n"+$_.value.ToString()
 }
 }
 }
 else {
 $hash.GetEnumerator() | ?{$_.value -ge 1} | %{$_.key}
 }
 }
}
#-------------------------------------------------
# Get all Contact details with WMI
#-------------------------------------------------
$DataArray = New-Object System.Collections.ArrayList
$UserInstanceID = Get-WmiObject -Class MSFT_SIPESUserSetting | ForEach-Object {$_.InstanceID}

foreach ($Contacts in $UserInstanceID){
$allcontacts = Get-WmiObject -Query ("Select * from MSFT_SIPESUserContactData where UserInstanceID = '$Contacts'")

 foreach ($f in $allcontacts) {

 $filteredAllcontacts = $allusers |
 where-object { $_.InstanceID -eq $f.UserInstanceID }

 foreach ($s in $filteredAllcontacts) {
 $objecttest = new-object PSObject @{
 ContactURI = $f.SIPURI; PrimaryURI = $s.PrimaryURI}

 $out = new-object psobject
 $out | add-member noteproperty PrimaryURI $s.PrimaryURI
 $out | add-member noteproperty Contacts $f.SIPURI
 $out | add-member noteproperty UserInstanceIDContacts $f.UserInstanceID

 $DataArray += $out

 }
 }
}
#Remove SIP URIs without contacts
$DataArray2 = $DataArray | Where-Object {$_ -notlike "@{PrimaryURI=; Contacts=; UserInstanceIDContacts=}"}

$col1 = $DataArray2 | ForEach-Object { $_.PrimaryURI }
$col2 = $DataArray2 | ForEach-Object { $_.Contacts }
$col3 = $DataArray2 | ForEach-Object { $_.UserInstanceIDContacts }

$obj = new-object psobject
$obj | add-member noteproperty PrimaryURI $col1
$obj | Add-Member noteproperty Contacts $col2
$obj | Add-Member noteproperty InstanceID $col3

#Count Duplicates
$hashT = @{}
$hashT = get-duplicate $obj.PrimaryURI -count

#sort array based on SIP URI
$Extract = $hashT | ForEach-Object { $_.Contact } | Sort-Object

#Remove SIP URIs from array
[array] $in = [Regex]::Replace($Extract,"(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b","");
$n = [regex]::Split( $in, "\s" )

#Remove empty values from array
$in2 = @($n | Where-Object {$_ -ne ''})
#-------------------------------------------------
# Count PIC Contacts
#-------------------------------------------------
$total = $in2.count
$i = 0
$value1 = 0
$j = 0
[int] $value2 = 0
$value2 = $in2[$j]
$countYahoo = 0
$countMSN = 0
$countAOL = 0
$TotalPIC = 0

While ($i –lt $total)
{
write-host "PrimaryUri:" $obj.PrimaryURI[($value1)]
$DisplayContacts = $obj.Contacts[($value1)..($value2-1)]
$numberOfContacts = $DisplayContacts.count
#-------------------------------------------------
# count Yahoo!
#-------------------------------------------------
$allyahoo = $DisplayContacts -like "*yahoo.com"
$allyahoo | ForEach-Object {$countYahoo++}
#-------------------------------------------------
# count MSN
#-------------------------------------------------
switch -wildcard ($DisplayContacts)
{
 "*live.*" {$countMSN++}
 "*hotmail.*" {$countMSN++}
 "*msn.com" {$countMSN++}
 "*livemail.*" {$countMSN++}
 "*messengeruser.com" {$countMSN++}
 "*passport.com" {$countMSN++}
 "*webtv.net" {$countMSN++}
 "*sympatico.ca"{$countMSN++}
}
#-------------------------------------------------
# count AOL
#-------------------------------------------------
switch -wildcard ($DisplayContacts)
{
 "*aol.com" {$countAOL++}
 "*icq.com" {$countAOL++}
 "*love.com" {$countAOL++}
 "*mac.com" {$countAOL++}
}
#-------------------------------------------------
# Display results
#-------------------------------------------------
$TotalPIC = $countYahoo+$countMSN+$countAOL
write-host "--Total number of contacts:$numberOfContacts"
write-host "--PIC contacts:$TotalPIC - Yahoo!=$countYahoo - MSN=$countMSN - AOL=$countAOL"
#write-host "Contacts:"
#$DisplayContacts
write-host "`n"

$value1 = $value2
$j++
$value2 = $value2+$in2[$j]
$i++
$countYahoo = 0
$countAOL = 0
$countMSN = 0
$TotalPIC = 0
}

It is also possible to view the contacts for each SIP URI when you uncomment line 136 & 137:

note: SIP URIs without contacts are not displayed.

Advertisements
  1. Dino Caputo
    March 10, 2011 at 10:13 pm

    Thanks Joris. When I ran the script I got the following error:

    PS C:\Scripts> .\OCSCount.ps1
    You must provide a value expression on the right-hand side of the ‘+’ operator.
    At C:\Scripts\OCSCount.ps1:20 char:46
    + Contact = $_.key.ToString().substring(4)+& <<<< quot;`n"+$_.value.ToString()

  2. Joris
    March 11, 2011 at 9:16 am

    It seems that the powershell code block in wordpress distorted the quote characters. Can you try it again?

  3. Dino Caputo
    March 11, 2011 at 3:33 pm

    Thanks. Downloaded script and this time getting following error:

    Get-WmiObject : Unexpected error
    At C:\Scripts\OCS_GetContacts.ps1:37 char:29
    + $allcontacts = Get-WmiObject <<<< -Query ("Select * from MSFT_SIPESUserContactData where UserInstanceID = '$Contacts

    The error loops.

  4. Joris
    March 11, 2011 at 4:36 pm

    Thx for the info. I think this error is generated due users without contacts.
    The WMI query fails because he could not find any contact that matches the UserInstanceID of the primaryURI. I will try to catch this error in the script.

    • Dino Caputo
      March 16, 2011 at 4:50 pm

      Joris, we do run multiple OCS pools so in a multdomain AD – not sure if this is contributing to the issue. We do have many users enabled for PIC that don’t have any contacts. The intial script that MS provided works great in counting the numbers however we need to identify whom isn’t using it so we can disable their access. Regards,
      Dino

  5. Joris
    March 16, 2011 at 6:49 pm

    If I could catch the error correctly I can Identify the users whom isn’t using OCS (without contact data). I have already opened a topic in the Powershell community forum because I am struggling with this error: http://powershellcommunity.org/Forums/tabid/54/aft/6334/Default.aspx

    I will keep you up-to-date if there is any progress.

  6. Yogesh
    October 19, 2011 at 10:45 pm

    When I run the script in powershell. Nothing happens, it brings up blank powershell screen.
    PLease suggest.

    Thanks

  7. Yogesh
    October 27, 2011 at 9:41 pm

    Hi Joris,

    I am getting this error while running script, please help

    Get-WmiObject :
    At C:\CIP.ps1:27 char:29
    + $allcontacts = Get-WmiObject <<<< -Query ("Select * from MSFT_SIPESUserContactData where UserInstanceID = '$Contacts
    '")
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

  8. Rob
    February 29, 2012 at 10:20 pm

    Greetings;

    Was wondering if this script will work against OCS 2007 R1. I need to know who’s using which PIC domains primarily AOL & Yahoo.

    Thx, Rob

  9. carlo
    July 13, 2012 at 1:58 pm

    Is there any one that know the equivalent of this script for Lync:

  1. No trackbacks yet.

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

%d bloggers like this: