从用户名列出用户详细信息 [英] List user details from Username

查看:153
本文介绍了从用户名列出用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本将检查用户名列表并显示用户全名和AD中的某些属性设置。基本上,我已经收到了一个用户名列表,该用户名只是数字,管理部门想知道每个用户名的用户全名。他们还想知道他们需要工作的部门。

I am trying to create a script that will check a list of user names and show the user full name and some attribute settings from AD. Basically I have been sent a list of usernames which are just numbers and management want to know the users full name for each username. they also want to know want division they work for.

下面是我创建的无法使用的脚本。

Below is the script I have created which doesn't work.

$csv = Import-Csv "C:\temp\users.csv"

foreach ($user in $csv) {
    $name = $user.myid
    Get-ADUser -Filter {EmployeeID -eq $name} -Properties * |
        Get-ADUser -Division $user.Programme
} | Export-Csv "C:\Temp\Results.csv"


推荐答案

如有疑问,请阅读文档 Get-ADUser 没有参数 -Division 。您需要选择输出文件中所需的属性。此外, foreach 循环不会将输出传递到管道中。如果要将输出直接传递到 Export-Csv

When in doubt, read the documentation. Get-ADUser doesn't have a parameter -Division. You need to select the properties you want in the output file. Also, foreach loops don't pass output into the pipeline. You need a ForEach-Object loop if you want to pass the output directly into Export-Csv:

Import-Csv 'C:\temp\users.csv' |
    ForEach-Object {
        $name = $_.myid
        Get-ADUser -Filter "EmployeeID -eq $name" -Properties *
    } |
    Select-Object SamAccountName, DisplayName, Division |
    Export-Csv 'C:\Temp\Results.csv' -NoType

否则,您需要将输出收集到变量中:

Otherwise you need to collect the output in a variable:

$users = foreach ($user in $csv) {
    $name = $user.myid
    Get-ADUser -Filter "EmployeeID -eq $name" -Properties *
}
$users | Export-Csv 'C:\Temp\Results.csv' -NoType

或运行循环在子表达式中:

$(foreach ($user in $csv) {
    $name = $user.myid
    Get-ADUser -Filter "EmployeeID -eq $name" -Properties *
}) | Export-Csv 'C:\Temp\Results.csv' -NoType

这篇关于从用户名列出用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆