简体   繁体   中英

get samaccountname from first and last name

I m trying to use a PowerShell script to ask for name and last name and then display the samAccountname I tried this and it did not work.

$Names = Read-host "Jack Robinson"
$Usernames = 
    Get-ADUser -Filter "FirstName -eq $($_.FirstName) -and Surname -eq $($_.LastName)" -Properties 'SamAccountName' |
        Select-Object -ExpandProperty 'SamAccountName'

In terms of taking input from a csv see @wasif Hasan's helpful answer. In terms of your particular code with no csv input - you are better off separating your request for first name and surname with two read-host requests. Take note of the attribute names GivenName for firstname and Surname for Last name

$firstname = read-host "first name"
$LastName = read-host "LastName"
Get-ADUser -Filter "GivenName -eq '$FirstName' -and Surname -eq '$LastName'"|
Select-Object -ExpandProperty 'SamAccountName'

Alternatively if you are unable to double the read-host requests as shown above you can attempt to filter on displayname which usually consists of firstname and lastname

$name = read-host "name"
Get-ADUser -Filter "Displayname -eq '$Name'"|
Select-Object -ExpandProperty 'SamAccountName'

Not sure why you used the pipeline variable when you are not taking input from anywhere. Also in AD user object Firstname is called GivenName and Lastname is called Surname. If you are taking input from csv like this:

FirstName,LastName,
xxx,xxx,
...

then try this:

$usernames = Import-Csv "filepath.csv" -Header GivenName,Surname -Delimiter "," | ForEach {
  Get-ADUser -Filter {GivenName -like $_.GivenName -and Surname -like $_.Surname} | Select-Object -ExpandProperty sAMaccountName 
}

I assume "Jack Robinson" is supposed to be a sample input. As you have it, it is the prompt that Read-Host will show the user. You probably want something like this:

$Names = Read-Host "Enter the name"

Separating the first and last names based on spaces is never foolproof, especially in cases of two or three-word last names. So you should either separate the prompt into first and last, like Itchydon's answer shows, or you can use ambiguous name resolution (ANR), which would let you search by the full name. That would look something like this:

$Names = Read-Host "Enter the name"
$Usernames = 
    Get-ADUser -LDAPFilter "(anr=$Names)" -Properties 'SamAccountName' |
        Select-Object -ExpandProperty 'SamAccountName'

Searching by first and last name is never an exact science, since you could have multiple people in your organization by the same name, but ANR does make that a little worse because it does a "contains" kind of search. So if you search for my name, "Gabriel Luci", for example, you would also find "Gabriele Luciano".

It just depends on your use. If your user will be picking the right account, then ANR is an easy way to find accounts. But if this is part of some automation where you want to match names exactly, then you will have to split up the first and last names at the input and search for exact matches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM