1

I'm currently writing a Powershell script that's used to select a user from Active Directory, then allow you to select a computer they've logged into (via an SQL query) and remote desktop into it. The user is prompted to enter a full or partial name, then a list of all matches are printed and they are prompted to select one. The list of all matches is from iterating through an array, which all matches have been assigned to. If the search from the name input produces an array with only one person, and then the user selects that one person I get the following error:

    Get-ADUser : Variable: 'u' found in expression: $u is not defined.
    At C:\Users\styanc\Desktop\test.ps1:67 char:12
    +     Get-ADUser <<<<  -f{DisplayName -eq $u} -Properties TelephoneNumber, OtherTelephone, Mobile | Select TelephoneNumber, OtherTelephone, Moblie #| Format
    -List
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : Variable: 'u' found in expression: $u is not defined.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
The functions are as follows.
    Function FindUsers{
        param ($n)
        #creates an array of users with names LIKE the script users input
        $n = @(Get-ADUser -f {DisplayName -like $n} -Properties DisplayName) 
        return $n
    }
    Function PrintUsers{
        param ($array)
        $i = 1
        #for each user in the array, print their name
        foreach($object in $array){
            Write-Host "$i. $($object.name)"
            $i++
        }
    }
    Function SelectUser{
        #there's probably a better way to do newlines?
        Write-Host ""
        #user selects a user from the list by number, input needs validation
        $userNum = Read-Host "Please select a user. (by number)"
        $length = $usersArray.Length
        Write-Host $length
        Write-Host $usersArray.length
        if($usersArray.Length -eq $null){
            $user = ($usersArray.Name)
        }
        else{
            $user = ($usersArray[$userNum-1].Name)
        }
        #$user = ($usersArray[$userNum-1].Name)
        return $user
    }

And are called like this:

$usersArray = FindUsers -n $name
PrintUsers -array $usersArray
$selectedUser = SelectUser

1 Answer 1

1

In function FindUsers, just try to replace :

return $n

by

return ,$n

,$n force return command to return a list whatever $n is a single value or a list, without that return command is in the habit to transform single value array into a single value.

Sign up to request clarification or add additional context in comments.

4 Comments

What a strange fix, what exactly does adding the comma do?
First this is not a bug, but a feature ;o) I add the explanation to the answer
Ahh ok, thanks! Makes sense now. I'm not entirely sure on all the workings of Powershell, I program in C and Java. This is my first time scripting.
The way you treat your question is not fair. You'd better split it into two questions. The first one not italicazed and flaged as answered with my answer and report the second question to a new one adding SQL or databinding flag. you'll have got a better chance to meet resolution for both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.