locked
PowerShell -Identity in get-aduser is null or empty on command when using a variable RRS feed

  • Question

  • I am fairly new to PowerShell and have a script that parts works via the command however not when ran as a script.  The problem appears when a variable assinged from a previous function is used in a second function inside of a for loop.  I can echo the variable and see the correct value.  However, I continue to see the following output:

    LMikula
    KNelson
    TCoyne
    JSpeedone
    BVanpay
    ---
    ---
    samaccountname
    --------------
    ---
    rchen
    ---
    tgundersen
    ---
    122
    2
    rchen
    Get-ADUser : Cannot bind parameter 'Identity' to the target. Exception setting
    "Identity": "Cannot validate argument on parameter: 'Identity'. The argument is
     null or empty. Supply an argument that is not null or empty and then try the c
    ommand again."
    At C:\Users\dsawyer\Desktop\psScript2.ps1:33 char:41
    +     get-aduser -server ks001-dc1  -Identity <<<<  $memberID -properties emplo
    yeeNumber
        + CategoryInfo          : WriteError: (:) [Get-ADUser], ParameterBindingEx
       ception
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
       .Management.Commands.GetADUser
    PS C:\Windows\system32>

    This is what the script looks like.

    import-module activedirectory
    set-executionpolicy -scope CurrentUser unrestricted
    set-executionpolicy unrestricted
    cls
    $members = @(Get-ADGroupMember 'EWA Employee Lookup' | ft samaccountname)
    echo $members
    echo "---"
    echo $members[0]
    echo "---"
    echo $members[1]
    echo "---"
    echo $members[2]
    echo "---"
    echo $members[3]
    echo "---"
    $counter = 2
    echo $members.Length
    for($counter; $counter -lt $members.Length; $counter ++)
    {
     	$memberID = [string]$members[$counter].value
    	echo $counter
    	echo $members[$counter]
    	echo $memberID.value
    	echo $memberID
    	get-aduser -server ks001-dc1  -Identity $memberID -properties employeeNumber
    	
    break
    }
    break

    I am guessing it is something simple.  Please help, thank you.

    - Dale


    x

    Friday, April 5, 2013 3:55 PM

Answers

  • Hi,

    The error is coming from the get-aduser cmdlet and is informing you that its -identity parameter is null. Just like if you type this:


    get-aduser -identity ""

    Bill

    • Marked as answer by OleDogNewTrix Friday, April 5, 2013 6:40 PM
    Friday, April 5, 2013 5:00 PM

All replies

  • Here's a much simpler way. Take advantage of the pipeline:


    get-adgroupmember 'EWA Employee Lookup' | get-aduser -properties employeeNumber

    Bill

    • Proposed as answer by Nicki Faulk Monday, May 6, 2019 2:11 PM
    Friday, April 5, 2013 4:00 PM
  • Thank you Bill.  That is a more straight forward approach. 

    I am still interested in why the other way does not work and throw the error, since I will likely use a variable with the various commands.


    x

    Friday, April 5, 2013 4:41 PM
  • Hi,

    The error is coming from the get-aduser cmdlet and is informing you that its -identity parameter is null. Just like if you type this:


    get-aduser -identity ""

    Bill

    • Marked as answer by OleDogNewTrix Friday, April 5, 2013 6:40 PM
    Friday, April 5, 2013 5:00 PM
  • True.  However the echo line before this shows that the value is not null or empty.  Is there something wrong with the way the variable $memberID is use with the cmdlet line?

    Dale


    x

    Friday, April 5, 2013 5:03 PM
  • If $memberID has content, specifically a string that matches a username, and the fails inside the get-aduser cmdlet that doesn't make a ton of sense.  Be totally certain that $memberID has the right type of content.  I'd put it in the ISE and set a breakpoint right before you do get-aduser and just be sure it has content and that that content is a string.

    Hope that helps! Jason

    Friday, April 5, 2013 5:09 PM
  • Good debug advise.  Doesnt the code snipplet direcly beofre the error demonstrate the correct data and type?

    $memberID = [string]$members[$counter].value
    echo $counter
    echo $members
    [$counter]
    echo $memberID
    .value
    echo $memberID



    x

    Friday, April 5, 2013 5:15 PM
  • The way you create $memberID is not consistent with anything I'm used to.

    I assume that this, "$memberID = [string]$members[$counter].value" creates a string but I'm not sure.  If get-aduser is saying $memberID is $null then it's not wrong.  Also you don't need to use echo at all in powershell, the code below will do what you're trying to do with the echo.

    $memberID = [string]$members[$counter].value
    	$counter
    	$members[$counter]
    	$memberID.value
    	$memberID
    	

    Also why are you putting [counter] on the right side of your variable?  I've never seen that before.

    If you want to write some output you can use write-output, also if it's a string you can just drop it out and the shell will write it.

    "Hello World"

    Will pop out exactly the way you expect.


    Hope that helps! Jason


    Friday, April 5, 2013 5:43 PM
  • That is good to know that I do not need to use echo. 

    The first line here is simply trying to cast the value to a string, so it can be used with the cmdlt.

    $memberID = [string]$members[$counter].value
    $counter

    eairlier in the code I assigned the get members results to an array.  This was my way of verifying the results were the same.

    $members[$counter]
    $memberID
    .value
    $memberID

    Who can argue with "If it is saying there is an error, there must be..."  Yet the value shows prior to the call.  I am just saying, it looks to me like there is a valid value, so why does it say there is not... it is a mystery.


    x

    Friday, April 5, 2013 5:49 PM
  • What happens when you send $MemberID to get-member?  If you don't mind paste the output into the forum.

    Hope that helps! Jason

    Friday, April 5, 2013 5:54 PM
  • The above code does use $memberID with get-aduser.   See code block and output of original question.


    x

    Friday, April 5, 2013 6:19 PM
  • I think it's much better to learn the PowerShell way (pipeline and object properties) than to try to manually iterate collections, etc. like you're trying to do.

    Bill

    Friday, April 5, 2013 6:30 PM
  • Thanks, Bill.  Your code was very helpful.  I agree. It just looked like the way I would process the command.  Probably since I am a programmer.  I will grin and ignore the fact that it appears there is a bug in the cmdlt when it parses the variable.  Thanks for all you help.

    x

    Friday, April 5, 2013 6:40 PM
  • Hi Bill.  I checked my AD and you are right all of my user objects show {} for the identity.  Is there a way to set them all to the samaccountname so that I can use the piped input for set-aduser?  I have a lot of scripts to write and unless I can pipe input to set-aduser they aren't going to happen.  

    Thank you

    Erik

    Wednesday, August 30, 2017 10:15 PM
  • Hi Bill.  I checked my AD and you are right all of my user objects show {} for the identity.  Is there a way to set them all to the samaccountname so that I can use the piped input for set-aduser?  I have a lot of scripts to write and unless I can pipe input to set-aduser they aren't going to happen.  

    Thank you

    Erik

    I've just come across this - and I know the thread is old - but I was using the Import-CSV cmdlet and got the error in the original post.

    It appeared that I had some empty fields in my CSV (ie lines where there had previously been text, but been cleared).  I had to select a few rows of the CSV in Excel, and delete the rows.  Error Fixed

    In case anyone else comes across it

    Thursday, October 25, 2018 3:04 PM