简体   繁体   中英

Powershell: import-csv and get-aduser

I trying to find users, storing in 'users.csv' in AD:

Import-Module ActiveDirectory
import-csv "\users.csv" | ForEach-Object {
Get-ADUser -Filter {displayname -eq $_.id}}

But PS says that 'id' property not found

users.csv contents:

id
MacAskill Danny
Cedric Gracia

etc

Edit the CSV file and enclose the displaynames with double quotes like so,

id
"MacAskill Danny"
"Cedric Gracia"

After that, use an itermediate variable like so,

Import-Csv .\users.csv | % {
    $f = $_.id; # Set the displayname into a temp variable
    get-aduser -filter { displayname -eq $f } # Use the temp variable with filter
}

I don't konw why an intermeidate variable is needed. There is something weird going on with passing and parsing variables with the -filter parameter.

# Use Import-csv and Get-ADUser together
# Import csv that contains "sn" column and get all AD users where
# sn matches any of the values imported from csv
Import-Csv C:\temp\1.csv | select sn -ExpandProperty sn | foreach { Get-ADUser -Filter 'sn -eq $_' }

I know this is an old post, but it really saved me a lot of headaches. Couldn't figure out how to find the user with imported values, but just by creating a intermediate variable, I was able to get my script to work. I didn't even need to use any double quotes.

Kris

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