Tuesday, July 29, 2014

Get-CSUserForwards

I was asked recently to come up with the best way to figure out which users were forwarding their land-line to another number (e.g. mobile) or to another user.

I came up with the idea of using SEFAUtil to figure out all the currently enabled users' setting.

# Initialize Variables
$AllUsers = @()
$Count = 0

# Define Location of SEFAUtil
$SEFAUtil = "C:\Program Files\Microsoft Lync Server 2013\ResKit\SEFAUtil.exe"

# Get Application Pool Server to use
$LyncServer = Get-CsTrustedApplication | ?{$_.ApplicationId -eq "urn:application:sefautil"}|%{$_.TrustedApplicationPoolFqdn}

# Get Lync EV Enabled Users
$LyncUsers = Get-CsUser | where {$_.EnterpriseVoiceEnabled -eq $true}
$TotalUsers = $LyncUsers.Count

# Loop for each one and get details
Foreach ($LyncUser in $LyncUsers){
    $Destination = $Null
    
    #Convert User SIP to lowercase
    $UserSIP = $LyncUser.SipAddress.ToLower().Replace("sip:","")

    #Write Progress
    $Count += 1
    Write-Progress -Activity "Processing Users" -status "User $UserSIP" `
        -percentComplete ($Count / $TotalUsers*100)

    #Do the magic
 &$SEFAUtil /Server:$LyncServer $UserSIP| Tee-Object -Variable output|Out-Null

    #Split the output to lines & convert to an object
    $parts = $output -split "`n"

    #Figure Out Destination  
  $Destination = ($parts[4] -split "to: ")[1]
 If ($Destination -eq "") {
    } ElseIf ($Destination -Match "user=phone") {
     # Destination is a number
  $Destination = $Destination.Split("@")[0].Split(":")[1]
 } Else {
  # Destination is a SIP address
        write-output $Destination
  #$Destination = $Destination.Split(":")[1]
 }

    #Display information
    $Data = New-Object -Type PSObject -Property @{
        SIP = ($parts[0] -split ": ")[1]
        Name = ($parts[1] -split ": ")[1]
        EVEnabled = ($parts[2] -split ": ")[1]
        SimRing = ($parts[3] -split ": ")[1]
        ForwardTo = $Destination
    }
    $AllUsers += $Data
}

Your feedback is always welcome.