0

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.

Matt
  • 45,022
  • 8
  • 78
  • 119
TheInfamousOne
  • 175
  • 1
  • 4
  • 17
  • 1
    Possible duplicate of [Function return value in PowerShell](http://stackoverflow.com/questions/10286164/function-return-value-in-powershell) – Matt Jan 29 '16 at 16:35
  • The dupe explains the core concepts here and the top answer has the technet reference as well. – Matt Jan 29 '16 at 16:35
  • Return keyword is _kinda_ redundant. The function will return all values send though output up until that statement. @MathiasR.Jessen :) I'm not perfect – Matt Jan 29 '16 at 16:39
  • That's perfect enough for me @Matt ;-) – Mathias R. Jessen Jan 29 '16 at 16:56

1 Answers1

2

In PowerShell, it is not only a function's return statement that creates output:

  • any statement in the function can write to the output stream.
  • furthermore, any statement that produces output by default writes to the output stream, unless that output is captured in a variable or suppressed (via >$null / | Out-Null or via $null = ...)

In your case it is the following statement (which returns the index at which $i is inserted into the StringCollection) instance:

$objSearcher.PropertiesToLoad.Add($i)

Simply suppressing its output will remove the 0, 1, 2, values from your output:

$null = $objSearcher.PropertiesToLoad.Add($i)

As an aside, you could simplify your code:

  • Use $env:USERNAME in lieu of (get-WmiObject win32_process -Filter "Name='explorer.exe'"|Select -First 1).GetOwner().User
  • Use the ActiveDirectory PowerShell module with its Get-ADUser cmdlet, which makes working with AD much easier.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I appreciate the help mklement0. This worked perfectly. I'm sorry about the formatting. I'll do a better job next time. I would love to use the Get-ADuser cmdlet. But our environment only has PowerShell 2.0 on the machines and on top of that, most of those users don't have the Administration tool installed on those systems that imports those commands on them. This script will be running on machines that don't have that command installed natively. – TheInfamousOne Jan 29 '16 at 16:59
  • @TheInfamousOne: Glad to hear it worked. No worries re formatting - the important part is to get into the habit of doing it correctly (which is why I encouraged _you_ to do it instead of fixing it myself). Understood re availability of the module. – mklement0 Jan 29 '16 at 17:03
  • @TheInfamousOne [Compare the markdowns](http://stackoverflow.com/posts/35089228/revisions) of my edit against your question and you can see the difference. Mainly when you copy in code an easy thing to do is hit CTRL+K and that will put in the indents that allow for code formatting. – Matt Jan 29 '16 at 17:23
  • Thanks, @Matt - never knew about (or, more likely: forgot about) Ctrl+K: awesome - even works with selecting existing text. For the sake of completeness, here's the [link](http://meta.stackexchange.com/a/22189/248777) with general code-formatting instructions again. – mklement0 Jan 29 '16 at 17:30
  • @Matt: I have to make a change but I'm not sure how to deal with this. I need to change this: [string]$sipaddress = $objItem.mail to this: [string]$sipaddress = $msRTCSIP-PrimaryUserAddress the problem is that it PowerShell does not like the '-' in msTRCSIP-PrimaryUserAdress. Is there a way I can change this to work? I think powershell thinks this is a commandlet because I'm trying to return the AD Attribute. – TheInfamousOne Feb 08 '16 at 13:52
  • @TheInfamousOne: I'm unclear on what `$msRTCSIP-PrimaryUserAddress` is - `msRTCSIP-PrimaryUserAddress` is not a valid variable name, and PowerShell will think you're trying to _subtract_ `PrimaryUserAddress` from variable `msRTCSIP`. Did you mean to _concatenate_ variable reference `$msRTCSIP` and literal `-PrimaryUserAddress`? Then put the expression in double quotes: `"${msRTCSIP}-PrimaryUserAddress"`. I suggest you ask a _new_ question if this doesn't help. – mklement0 Feb 08 '16 at 14:41
  • I asked this question in another link. Sorry, you can see it here. A user named BaconBits helped me figure it out. I was trying to avoid creating another question and wanted to add it here. But I thought I would start new from the beginning. (see link below) [link]http://stackoverflow.com/questions/35272640/powershell-querying-ad-attribute-unexpected-token-in-expression-or-statement – TheInfamousOne Feb 08 '16 at 19:26