从仅包含ID的文本文件中,使用PowerShell将AD用户属性导出到CSV文件

huangapple go评论66阅读模式
英文:

Exporting AD users' attributes to CSV file from a text file with only IDs in powershell

问题

I'm using PowerShell, and I'm trying to export AD users' attributes to a CSV file from a text file that contains only user IDs (samAccountName). The text file can contain any amount of user IDs.

The problem is that when I run the code I have, the file created only has 1 entry, and it's always the last user in the list of users in the text file, regardless of the amount of users I try to get the info for.

The code I run is this:

$Users = Get-Content "C:\IDs.txt" |
ForEach-Object {Get-ADUser -Identity $_ -Properties * | Select Name,SamAccountName,Description,Title,DistinguishedName,Enabled | Export-Csv "c:\UserInfo.csv" -NoTypeInformation}

Thank you in advance for your help.

英文:

I'm using PowerShell, and I'm trying to export AD users' attributes to a CSV file from a text file that contains only user IDs (samAccountName). The text file can contain any amount of user IDs.

The problem is that when I run the code I have, the file created only has 1 entry, and it's always the last user in the list of users in the text file, regardless of the amount of users I try to get the info for.

The code I run is this:

$Users = Get-Content "C:\IDs.txt" |
ForEach-Object {Get-ADUser -Identity $User -Properties * | Select Name,SamAccountName,Description,Title,DistinguishedName,Enabled | Export-Csv "c:\UserInfo.csv" -NoTypeInformation}

Thank you in advance for your help.

答案1

得分: 0

以下是翻译好的部分:

以下陈述将无法正常工作:

$Users = Get-Content "C:\IDs.txt" | ForEach-Object {
Get-ADUser -Identity $User -Properties * ....


因为 `$User` 未定义,在 `ForEach-Object` 循环中,要引用管道中的当前项目,您应该使用 [`$_` (`$PSItem`)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psitem?view=powershell-7.2)。

至于您在导出中获得最后一个元素的原因,您之前在测试代码时已定义了 `$User`,然后在 `C:\IDs.txt` 中的每一行中查询相同的 AD 对象,由于 `Export-Csv` 位于循环内,它一遍又一遍地被覆盖。

修复它的步骤非常简单,只需将 `Export-Csv` 放在管道中的最后一个语句,并将 `$User` 更改为 `$_`:

$props = 'Name', 'SamAccountName', 'Description', 'Title', 'DistinguishedName', 'Enabled'
Get-Content "C:\IDs.txt" |
ForEach-Object { Get-ADUser -Identity $_ -Properties $props } |
Select-Object $props |
Export-Csv "c:\UserInfo.csv" -NoTypeInformation


此外,对于 `$Users` 的分配 (`$Users = ...`) 会始终为 null,因为 `Get-ADUser` 的输出被定向到 `Export-Csv`。

最后,由于 `Get-ADUser` 可以处理来自管道的标识,不需要 `ForEach-Object`:

$props = 'Name', 'SamAccountName', 'Description', 'Title', 'DistinguishedName', 'Enabled'
Get-Content "C:\IDs.txt" | Get-ADUser -Properties $props |
Select-Object $props |
Export-Csv "c:\UserInfo.csv" -NoTypeInformation

英文:

The following statement wouldn't work:

$Users = Get-Content "C:\IDs.txt" | ForEach-Object {
    Get-ADUser -Identity $User -Properties * ....

Because $User is not defined, in a ForEach-Object loop, to refer to the current item from the pipeline, you should use $_ ($PSItem).

As for the likable cause you get the last element in your export, you have previously defined $User while testing your code, then you're querying the same AD Object for each line in C:\IDs.txt and the export, since Export-Csv is inside the loop, is being overwritten over and over.

The steps to fix it are quite straight forward, just put Export-Csv as the last statement in your pipeline and change $User for $_:

$props = 'Name', 'SamAccountName', 'Description', 'Title', 'DistinguishedName', 'Enabled'
Get-Content "C:\IDs.txt" |
    ForEach-Object { Get-ADUser -Identity $_ -Properties $props } |
    Select-Object $props |
    Export-Csv "c:\UserInfo.csv" -NoTypeInformation

Also the assignment to $Users ($Users = ...) will be always null since the output from Get-ADUser is being directed to Export-Csv.

Lastly, since Get-ADUser can process identities from pipeline, no ForEach-Object is needed:

$props = 'Name', 'SamAccountName', 'Description', 'Title', 'DistinguishedName', 'Enabled'
Get-Content "C:\IDs.txt" | Get-ADUser -Properties $props |
    Select-Object $props |
    Export-Csv "c:\UserInfo.csv" -NoTypeInformation

huangapple
  • 本文由 发表于 2023年2月14日 09:29:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定