如何在 powershell 中为每个 Active Directory OU 用户更正我的 foreach 循环?

cae*_*tcs 2 windows powershell active-directory organizational-unit

目标:创建一个 for 或 foreach 循环来为 OU 中的每个用户执行一些代码(在本例中,只需打印 x)。我使用带有 ActiveDirectory 模块的 powershell 2.0。

到目前为止:这就是我所拥有的(见下文)。它只是为每个用户打印出 X。但它并没有像我想要的那样工作,相反,我认为它可能对每一行都这样做。所以我得到了 6 个 X,分别代表 'name'、'----'、'test1'、'test2'、SPACE、SPACE。

$pool = Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=Test,OU=Users,OU=jack,DC=Corp,DC=jill,DC=com" -Properties name | FT name
foreach ($user in $pool )
{ write-host "x"}
$pool
Run Code Online (Sandbox Code Playgroud)

结果,SPACE 将用句点 (.) 表示:

x
x
x
x
x
x


name                        
----                      
test1                  
test2
.
.
Run Code Online (Sandbox Code Playgroud)

我不确定它为什么这样做。如果您有更好的方法或方法来处理这个问题,我会很高兴听到的。

jsc*_*ott 5

$pool将包含Format-Table name第一行的最后一步的输出。的Format-*小命令用于在屏幕上显示的值。您几乎肯定不想将格式化的表格提供给您的foreach循环。

$pool = Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=Test,OU=Users,OU=jack,DC=Corp,DC=jill,DC=com"
foreach ($user in $pool) {
  Write-Host "x"
}

# And if you really want to see an `ft $pool`:
$pool | Format-Table name
Run Code Online (Sandbox Code Playgroud)