简体   繁体   中英

Csv empty to export custom attribute from ad users

From csv file, no problem in powershell console to see the value of the attribute but when I want to export in csv file, the file is empty, maybe someone could help me please ?

$adusers = Import-Csv "c:\temp\adusers.csv" -Delimiter ";"

foreach ($item in $adusers) {
# Map variables from CSV
$adusers = $item.'ADUsers'
$Servers = $item.'Domain'


Get-ADUser -identity $adusers -Properties flags -Server $Servers | Select- 
Object -Property samaccountname, userprincipalname, flags | export-csv 
c:\temp\fdfdrff.csv -Encoding Unicode

}

File adusers.csv:

ADUsers;Domain
account1;Domain1
account2;Domain2
account3;Domain3
account4;Domain4
account5;Domain5
account6;Domain6
account7;Domain7
account8;Domain8
account9;Domain9

The output file :

"#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser"
"samaccountname","userprincipalname","flags"
"account9","account9@Domain9.local",

If you use Export-CSV inside foreach loop, you have to remember about the -Append parameter. Otherwise, each time the file will be replaced which happens in your example.

Also, you can remove assigning the variables and use $item properties directly:

Get-ADUser -identity $item.ADUsers -Properties flags -Server $item.Domain

More detailed troubleshooting:

$adusers = Import-Csv "c:\temp\adusers.csv" -Delimiter ";"
$adusers

If $adusers doesn't show the objects correctly, check the file format.

Inside foreach run only

Get-ADUser -identity $item.ADUsers -Properties flags -Server $item.Domain

If AD users are not listed correctly, check if file contains proper identities and/or servers. Accepted identities and/server name formats can be found in Get-AdUser docs .


Based on the file you provided:

The output file :

 "#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser" "samaccountname","userprincipalname","flags" "account9","account9@Domain9.local", 

I can see two things:

  • You could use -NoTypeInformation to get rid of first line
  • As you can see only the last line that means getting data from AD was successful and you only need -Append in your Export-CSV .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM