arrays - 如何将活动目录数据导入数组并将其导出到单个 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 文件,但是它是空的。
解决方案
所以我看到你正在使用 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 的样本数据可能会有所帮助。
推荐阅读
- python - 我需要解析和存储来自 json 或 yaml 文件的值列表
- excel - 如何在数据透视表中选择 lastrow 之前的行?
- xml - 带有starts-with() 的XSLT Select 标记在数值上失败
- r - R2D3 与 D3.js 相比
- php - 将列分配给刀片中的变量时如何指定所需的表
- r - 根据闪亮条件更改表格单元格的格式
- php - PHP 中的 if ($check)、if (!$check) 和 if (!!$check) 有什么区别?
- python-3.x - 需要解决 Pandas“超出纳秒时间戳”错误的问题
- javascript - Firebase 功能不删除文档
- android - 如何在 Android 应用程序中设计这个看起来像 Spinner 但实际上不是的组件?