When I query AD return a SIP address. It's not a problem. the string comes back perfectly. But I will be constantly using this code, so I need to write a function. The function returns what I need, but it also returns additional things I don't need.
All I am doing is query AD and getting the sip address. eg:John.Smith@ourbusiness.com. I'm splitting the email address at the "@" symbol and returning what is to the left of it. Again, the code returns John.Smith perfectly.
But when I add this code inside a function, I get returns that looks like this.
0
1
2
John.Smith
Here is what I have.
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"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults){
$objItem = $objResult.Properties
[string]$UserName = $objItem.name
[string]$sipaddress = $objItem.mail
[string]$mailnickname = $objItem.mailnickname
}
$theSIP = $sipaddress.Split("@")[0]
return $theSIP
}
$mySIPaddress = CheckSip
All I need is john.smith
I'm sure I'm syntax handicapped somewhere, I just don't know where to look.