如何将活动目录数据导入数组并将其导出到单个CSV文件?

时间:2019-06-25 05:33:49

标签: arrays powershell csv active-directory

如何将活动目录数据(电子邮件地址和电话号码)导出到单个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文件,但它为空。

2 个答案:

答案 0 :(得分:0)

Get-ADUser 中的参数LDAPFilter不使用您那里的语法。

您需要使用-LDAPFilter“(displayName = $ user)”或保留现有语法并将参数更改为-Filter {DisplayName -eq $ user}

答案 1 :(得分:0)

所以我看到您正在使用csv。这意味着Get-content是cmdlet的错误选择。您必须使用Import-csv。 Csv还意味着您在该文件上具有标头。

enter image description here

所以当您这样做

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

您将在foreach loop的每次迭代中以这种格式获取返回的数据。

enter image description here

您需要的是这个。

 # 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的示例数据。