【问题标题】:How to import active directory data into an array and export that into a single CSV file?如何将活动目录数据导入数组并将其导出到单个 CSV 文件中?
【发布时间】:2019-11-06 21:36:08
【问题描述】:

如何将 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 文件正在创建,但它是空的。

【问题讨论】:

  • 尝试将$name 更改为$user
  • 谢谢德鲁,真不敢相信我错过了这么愚蠢的事情!不幸的是,这并没有帮助 CSV 导出仍然为空

标签: arrays powershell csv active-directory


【解决方案1】:

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

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

【讨论】:

  • 不幸的是,这两个都不起作用。将查询更改为 -LDAPFilter "(displayName=$user)" 会返回 Get-ADUser : Cannot find an object with identity: '-LDAPFilter (displayName=Heather Cosgriff)'。将查询更改为 -Filter {DisplayName -eq $user} 会返回 Get-ADUser : 无法识别搜索过滤器。谢谢
  • 根据错误消息,我认为您在脚本中添加了一些额外的引号或双引号。例如,无法找到具有标识的对象:'-LDAPFilter (displayName=Heather Cosgriff)' 似乎是调用“-LDAPFilter (displayName=$user)”而不是 -LDAPFilter “(displayName=$user)”的结果。我在发布之前测试了解决方案,所以如果您自己找不到问题,您可能需要发布以 $displaynamedetails = 开头的整行以供审核
【解决方案2】:

所以我看到您使用的是 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 的样本数据会有所帮助。

【讨论】:

  • @RohinSidarth 我正在使用 CSV 来处理 PowerShell 但是,没有标题,因此如果我是 Import-CSV,标题将是我的第一个用户名。但是,当我将 AD 查询更改为 $displaynamedetails = Get-ADUser -filter "DisplayName -eq '$user'" | Select name,samAccountName,OfficePhone 时,我收到以下错误 Get-ADUser :解析查询时出错:'DisplayName -eq 'Robyn O'Brien'' 错误消息:'syntax error' 在位置: '26' 谢谢
  • 顺便说一句,通过这些编辑,即使按照指南,我也无法让换行符工作。为了获得换行符,我在行尾留了 2 个空格正确吗?
  • @RohinSidarth 我对这个编辑器没有经验,无法链接图像,更不用说创建换行符了。我已经关注了您的 Edit2 并且它大部分都有效 - 我发现我的主要错误是用户名中带有撇号的用户,但已解决此问题。现在我的脚本运行没有错误,但仍导出空白 CSV。这可能是由于脚本击中了不存在的用户并退出了吗?如果是这样,我将如何添加检查以查看用户是否存在,如果他们不存在,或者该空间为空白以跳过并继续其余部分?
猜你喜欢
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 2016-01-04
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 2014-08-11
相关资源
最近更新 更多