TheInfamousOne February 2016

PowerShell: Querying AD attribute "unexpected token in expression or statement

I recently had to change my powershell script to query a SIP address in a different attribute in AD. But I'm not sure how to write it so I can query the "msRTCSIP-PrimaryUserAddress" in Active Directory. This is due to the '-' in the msRTCSIP-PrimaryUserAddress attribute. When I hover my mouse over the squiggly line, it says "unexpected token in expression or statement"

I can query other things from AD suchs as name, mailnickname etc. But I need this one specifically because of the way we are changing our SIP address.

Function CheckSIP {

    $loggedOnUser = (get-WmiObject win32_process -Filter "Name='explorer.exe'"|Select -First 1).GetOwner().User

    $strFilter = "(&(objectCategory=User)(mailnickname=$loggedOnUser))"
    $objDomain = New-Object 

    System.DirectoryServices.DirectoryEntry("LDAP://OU=Offices,dc=OurNetwork,dc=net")
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter
    $objSearcher.SearchScope = "Subtree"

    $colProplist = "name", "mail", "mailnickname", "msRTCSIP-PrimaryUserAddress"
    foreach ($i in $colPropList){$null = $objSearcher.PropertiesToLoad.Add($i)}

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults){

        $objItem = $objResult.Properties
        [string]$UserName = $objItem.name 
        [string]$mail = $objItem.mail
        [string]$mailnickname = $objItem.mailnickname
        [string]$sipaddress = $objItem.msRTCSIP-PrimaryUserAddress
    }

    $theSIP = $sipaddress.Split("@")[0]    
    return $theSIP
}

$mySIPaddress = CheckSip

I'm aware of the "Get-ADUser" command, but not all of our machines will have this commandlet natively on their machine. So that is why I'm doing it like this.

Answers


Bacon Bits February 2016

Remember, the hyphen is an operator. Try:

[string]$sipaddress = $objItem.'msRTCSIP-PrimaryUserAddress'

You also need to specify the property for the searcher:

$colProplist = "name", "mail", "mailnickname","msRTCSIP-PrimaryUserAddress";
$objSearcher.PropertiesToLoad.AddRange($colProplist);

I eliminated the unnecessary loop as well.


TheInfamousOne February 2016

Going by BaconBits suggestion and getting rid of the loop. Here is what the working function looks like.

Function CheckSIP {
$loggedOnUser = (get-WmiObject win32_process -Filter "Name='explorer.exe'"|Select -First 1).GetOwner().User
$strFilter = "(&(objectCategory=User)(mailnickname=$loggedOnUser))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Offices,dc=ourdomain,dc=net")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name", "mail", "mailnickname", "msrtcsip-primaryuseraddress";
$objSearcher.PropertiesToLoad.AddRange($colProplist);

    $colResults = $objSearcher.FindAll()
    $objItem = $colResults.Properties
    [string]$sipaddress = $objItem.'msrtcsip-primaryuseraddress'
    $theSIP = $sipaddress.Split("@")[0]
    $theSIP2 = $theSIP.Split(":")[-1] 
    return $theSIP2
    }

As you can see, I changed it to all lower case for msrtcsip-primaryuseraddress and it worked.

Thanks BaconBits!

Post Status

Asked in February 2016
Viewed 3,586 times
Voted 14
Answered 2 times

Search




Leave an answer