使用 Get-ADUser 时,如何将用户的域信息添加到 PowerShell 的管道中?

MDM*_*rra 3 windows powershell active-directory

我正在尝试枚举林中每个用户所属的名称、samaccountname 和域,并将其写入文本文件。

我现在拥有的脚本是:

Import-Module ActiveDirectory

$domains = "root.org", "child1.root.org", "child2.root.org"


ForEach ($d in $domains){

Get-ADUser -Filter * -ResultSetSize $null -Server $d -Properties name, samaccountname |
Select-Object name, samaccountname | out-file c:\users\mdmarra\desktop\users.txt -append

}
Run Code Online (Sandbox Code Playgroud)

我需要的是每行末尾的 $d 值,以便输出看起来像

name          samaccountname        domain
----          --------------        ------
Marra,Mark    mdmarra               root.org
Run Code Online (Sandbox Code Playgroud)

Bar*_*ekB 7

这是您可以使用哈希表轻松实现的事情Select-Object

ForEach ($d in $domains){

Get-ADUser -Filter * -ResultSetSize $null -Server $d -Properties name, samaccountname |
Select-Object name, samaccountname, @{ Name = 'domain'; Expression = { $d }} | out-file c:\users\mdmarra\desktop\users.txt -append

}
Run Code Online (Sandbox Code Playgroud)