簡體   English   中英

如何將活動目錄數據導入數組並將其導出到單個CSV文件中?

[英]How to import active directory data into an array and export that into a single CSV file?

如何將活動目錄數據(電子郵件地址和電話號碼)導出到單個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文件,但它是空的。

Get-ADUser中的參數LDAPFilter不使用您在那里的語法。

您需要使用-LDAPFilter“(displayName = $ user)”或保留現有語法並將參數更改為-Filter {DisplayName -eq $ user}

所以我看到你正在使用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的示例數據會很有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM