Expandproperty与表达式结合使用



im试图通过一个表获得用户的直接报告的良好输出。

下面的代码为我提供了输出,但它显示的是distinguishedName,而不是displayname。

Get-ADUser $user -Properties * | Select-Object -ExpandProperty DirectReports

我希望它是这样的,但有直接报告,而不是经理

Get-ADUser -Identity $user -Properties * | select-object @{N='Manager';E={(Get-ADUser ($_.Manager)).name}} | Format-List

我已经到了可以列出直接下属的地步,但他们不是一行接一行的,我只想显示名称

get-aduser -Identity $user -Properties DirectReports | Select-Object name, @{N='Direct reports';E="DirectReports"} | format-list

因为这:

get-aduser -Identity $user -Properties DirectReports | Select-Object name, @{N='Direct reports';E="DirectReports"} | format-list

不这么做。您要求的是列表,而不是柱状报告,即Csv.txt。这就是Export-Csv或哈希表PSCustomObject的用途。

你所追求的并不是什么新鲜事,因为这是一件常见的事情,网上和上面的SO搜索框都有很多例子。

PowerShell-谁向谁报告?(Active Directory递归DirectReports(

# Find all direct user reporting to Test_director
Get-ADDirectReports -Identity Test_director
# Find all Indirect user reporting to Test_director
Get-ADDirectReports -Identity Test_director -Recurse
Get-ADUser -Identity test_director -Properties directreports |
Select-Object -ExpandProperty directreports |
Get-ADUser -Properties mail |
Select-Object SamAccountName, mail

使用Powershell(递归(在Active Directory中获取直接报告

函数获取DirectReport{#需要-模块ActiveDirectory

<#
.SYNOPSIS
This script will get a user's direct reports recursively from ActiveDirectory unless specified with the NoRecurse parameter.
It also uses the user's EmployeeID attribute as a way to exclude service accounts and/or non standard accounts that are in the reporting structure.

.NOTES
Name: Get-DirectReport
Author: theSysadminChannel
Version: 1.0
DateCreated: 2020-Jan-28

.LINK
https://thesysadminchannel.com/get-direct-reports-in-active-directory-using-powershell-recursive -   

.PARAMETER SamAccountName
Specify the samaccountname (username) to see their direct reports.

.PARAMETER NoRecurse
Using this option will not drill down further than one level.

.EXAMPLE
Get-DirectReport username

.EXAMPLE
Get-DirectReport -SamAccountName username -NoRecurse

.EXAMPLE
"username" | Get-DirectReport
#>

[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]

[string]  $SamAccountName,

[switch]  $NoRecurse
)

BEGIN {}

PROCESS {
$UserAccount = Get-ADUser $SamAccountName -Properties DirectReports, DisplayName
$UserAccount | select -ExpandProperty DirectReports | ForEach-Object {
$User = Get-ADUser $_ -Properties DirectReports, DisplayName, Title, EmployeeID
if ($null -ne $User.EmployeeID) {
if (-not $NoRecurse) {
Get-DirectReport $User.SamAccountName
}
[PSCustomObject]@{
SamAccountName     = $User.SamAccountName
UserPrincipalName  = $User.UserPrincipalName
DisplayName        = $User.DisplayName
Manager            = $UserAccount.DisplayName
}
}
}
}

END {}

}

如何在Powershell 中从AD获得经理的直接和间接报告数量

相关内容

  • 没有找到相关文章

最新更新