首页 > 解决方案 > 如何将活动目录数据导入数组并将其导出到单个 CSV 文件中?

问题描述

如何将活动目录数据(电子邮件地址和电话号码)导出到单个 CSV 文件中?

我已将 Active Directory 数据导出到数组中并将数组导出到 CSV 文件中,但 CSV 文件显示为空

# Change directory to where the CSV files are located
cd "C:\Users\srobins\OneDrive - Ruralco Holdings Limited\Desktop"

# Declare an array and assign the list of users to this
$users = Get-Content users.csv

# Create an array for the active directory data to be stored
$displayname = @()

# Loop through the list of users
foreach ($user in $users) {

# Get the active directory data and store in a tempory variable
$displaynamedetails = Get-ADUser -LDAPFilter { DisplayName -eq $name }| Select name,samAccountName,OfficePhone

# Append the active direcotry data into the array
$displayname += $displaynamedetails

}

#export the array containing the list of active directory data into the new CSV
$displayname | Export-Csv "C:\SamAccountName.csv" -NoTypeInformation

我希望创建 CSV 文件“SamAccountName”,并将 Active Directory 数据列表存储在其中。

正在创建 CSV 文件,但是它是空的。

标签: arrayspowershellcsvactive-directory

解决方案


所以我看到你正在使用 csv。这意味着,Get-content是 cmdlet 的错误选择。你必须使用Import-csv. CSV 还意味着您在该文件上有一个标题。

在此处输入图像描述

所以当你这样做时

foreach ($user in $users) {
   $user
}

您将通过foreach loop.

在此处输入图像描述

你需要的是这个。

 # Change directory to where the CSV files are located
cd "C:\Users\srobins\OneDrive - Ruralco Holdings Limited\Desktop"

# Declare an array and assign the list of users to this
$users = Import-Csv users.csv

# Create an array for the active directory data to be stored
$displayname = @()

# Loop through the list of users
foreach ($user in $users.Name) #$users.Name has to reflect the header you have. In my case it was name.
{

# Get the active directory data and store in a tempory variable
$displaynamedetails = Get-ADUser -filter "DisplayName -eq '$user'" | Select name,samAccountName,OfficePhone #mind the '' around $user

# Append the active direcotry data into the array
$displayname += $displaynamedetails

}

#export the array containing the list of active directory data into the new CSV
$displayname | Export-Csv "C:\SamAccountName.csv" -NoTypeInformation

EDIT2----------------------------------------

我强烈建议添加标题,否则,为什么要使用 csv?如果不是这种情况,您必须进行一些不太理想的文本操作。

# Change directory to where the CSV files are located
cd "C:\Users\srobins\OneDrive - Ruralco Holdings Limited\Desktop"

# Declare an array and assign the list of users to this
$FileContent = Get-Content users.csv
$Users = $FileContent | ForEach-Object {($_.Split(",")[0]).trim()}
#[0] is the position of the name part in each line when it is split into parts at the comma. Eg: John Doe,32,blah
#In my case it was the 1st part. Change this according to ur data

# Create an array for the active directory data to be stored
$displayname = @()

# Loop through the list of users
foreach ($user in $users) #$users.Name has to reflect the header you have. In my case it was name.
{

# Get the active directory data and store in a tempory variable
$displaynamedetails = Get-ADUser -filter "DisplayName -eq '$user'" | Select name,samAccountName,OfficePhone #mind the '' around $user

# Append the active direcotry data into the array
$displayname += $displaynamedetails

}

#export the array containing the list of active directory data into the new CSV
$displayname | Export-Csv "C:\SamAccountName.csv" -NoTypeInformation

查看 csv 的样本数据可能会有所帮助。


推荐阅读