中每个用户的管理器名称

中每个用户的管理器名称

本文介绍了使用 powershell 检索 AD 中每个用户的管理器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 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

manager 的输出返回为:

The output for manager is returned as:

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"
}

当使用 Select-Object 时,您也可以在计算属性中使用它:


You could also use it inside a calculated property when using Select-Object:

$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 中每个用户的管理器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:52