使用powershell为AD中的每个用户检索管理器名称 [英] Retrieve Manager name for each user in AD using powershell
问题描述
我正在尝试将AD中的OU(员工)转储为特定格式
I am trying to dump an OU (Staff) in our AD to a specific format
"name" -> "Manager";
我正在清零,但是我碰到了下面的代码
I am zeroing in but I'm hitting a wall with the following code
get-aduser -filter * -SearchBase "OU=Staff,DC=whatever,DC=local" | get-aduser -Properties Manager | Select Name,Manager
经理的输出返回为:
CN=Sharon Doe,OU=Staff,DC=whatever,DC=local
我也不确定如何将文本用引号引起来,并在名称和管理者之间插入箭头
Also I am unsure how to wrap the text in quotes and insert the arrow between name and manger
如果可以指出我的话,谢谢朝着正确的方向
Thanks if you can point me in the right direction
这是我到目前为止的sudo工作代码
this is my sudo working code so far
Import-Module ActiveDirectory
$users = $null
$i = $null
$users = Get-ADUser -SearchBase "ou=Staff,dc=whatever,dc=local" -filter * ` -property description
ForEach($user in $users)
{
$user.name + >>>Get-ADUser($users.manager).name**<<<
$i++
}
"$i users"
推荐答案
您可以使用
(Get-ADUser "CN=Sharon Doe,OU=Staff,DC=whatever,DC=local").DisplayName
经理的用户对象,并获取 DisplayName
而不是DN。
to fetch the manager's user object and grab the DisplayName
instead of the DN.
如果您不自信使用计算出的属性(见下文),您可以在 foreach
循环:
If you don't feel confident working with calculated properties (see below), you can use it inside a foreach
loop:
$Users = Get-ADUser -filter * -SearchBase "OU=Staff,DC=whatever,DC=local" -Properties Manager
foreach($User in $Users){
$Manager = Get-ADUser $User.Manager -Properties DisplayName
$ManagerName = $Manager.DisplaýName
"$($User.Name) -> $ManagerName"
}
您也可以在计算中使用它使用选择对象
时的属性:
$Users = Get-ADUser -filter * -SearchBase "OU=Staff,DC=whatever,DC=local" -Properties Manager
$Users | Select Name,@{label="Manager";expression={(Get-ADUser $_.Manager -Properties DisplayName).DisplayName}}
如果 Select
语句变得难以理解,则始终可以使用 splatting 表,其属性为:
If the Select
statement gets too unreadable, you can always make a splatting table with the properties:
$NameManager = @{
"Property" = @(
"Name"
@{
Label = "Manager"
Expression = {
Get-ADUser $_.Manager -Properties DisplayName |Select -Expand DisplayName
}
}
)
}
$Users | Select-Object @NameManager
这篇关于使用powershell为AD中的每个用户检索管理器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!