简体   繁体   English

如何将2个属性合并到AD对象的单个输出中?

[英]How to merge 2 properties into single output of AD Objects?

I need to merge two properties into one column, name of user is in DisplayName while Name of group is stored in Name how ever both types of objects has DisplayName, Name properties so I need to show them in one column. 我需要将两个属性合并到一栏中,用户名在DisplayName中,而组名存储在Name中,两种对象都具有DisplayName,Name属性,因此我需要在一栏中显示它们。

Suppose Group is 'Test Group'

CN : …
ObjectGUID : 123-456-XXXX
ObjectClass: group
DisplayName: 
Name: 'Test Group'

And 'Test User' Properties are 
CN: …
ObjectGUID : 789-456-XXXX
ObjectClass: user
DisplayName: 'Test User'
Name: 

I have tried using a for each looping but can't figure out the use of select statement. 我已经尝试过为每个循环使用a,但无法弄清楚select语句的使用。

Get-ADGroupMember -Identity $GroupGUID | 
ForEach-Object{
    if($_.ObjectClass -eq 'User'){
        # process user object
        $_ | Get-ADUser -Properties DisplayName
    }elseif ($_.ObjectClass -eq 'User'){
        # process group
        $_ | Get-ADGroup -Properties Name
    }
} 

The expected output need to be 预期的输出需要是

MemberName          ObjectGUID
----------------    ------------------
Test Group          123-456-XXXX
Test User           789-456-XXXX

i think you are wanting to merge the sources into one output object. 认为您想将源合并为一个输出对象。

the usual way to add values from multiple sources into one object is to use a [PSCustomObject] to build a ... custom object . 将来自多个源的值添加到一个对象中的通常方法是使用[PSCustomObject]来构建... 自定义对象 [ grin ] i don't have any AD access, so this is done with local user & group info. [ 咧嘴 ]我没有任何广告访问权限,因此这是通过本地用户和组信息完成的。 you can do some fairly complex structuring by calculating what you want to work with and stuffing it all into one neatly structured object. 您可以通过计算要使用的内容并将其全部填充到一个整齐的结构化对象中,来进行一些相当复杂的结构化处理。

here's a demo of the idea using local accounts ... 这是使用本地帐户的想法演示...

$GroupName = 'Homonymic_Tuu'

$MemberList = Get-LocalGroupMember -Group $GroupName |
    Where-Object {$_.ObjectClass -eq 'User'}
$GroupInfo = Get-LocalGroup -Name $GroupName

foreach ($ML_Item in $MemberList)
    {
    $UserInfo = Get-LocalUser -Name $ML_Item.Name.Split('\')[-1]
    if ([string]::IsNullOrEmpty($UserInfo.LastLogon))
        {
        $LastLogon = '_Never_'
        }
        else
        {
        $LastLogon = $UserInfo.LastLogon
        }
    [PSCustomObject]@{
        Group_Name = $GroupInfo.Name
        Group_Description = $GroupInfo.Description
        User_Name = $UserInfo.Name
        User_Description = $UserInfo.Description
        User_Enabled = $UserInfo.Enabled
        User_LastLogon = $LastLogon
        }
    }

truncated output ... 截断的输出...

Group_Name        : Homonymic_Tuu
Group_Description : Homonym - sounds like Tuu
User_Name         : 22
User_Description  : The Digit 2 Twice
User_Enabled      : True
User_LastLogon    : 2018-11-27 9:19:10 PM

[*...snip...*] 

Group_Name        : Homonymic_Tuu
Group_Description : Homonym - sounds like Tuu
User_Name         : TwoTwo
User_Description  : Repeating the name of the number after One.
User_Enabled      : True
User_LastLogon    : _Never_

the data will also export to a CSV file quite easily. 数据也将很容易导出到CSV文件。 [ grin ] [ 咧嘴 ]

I'd use a switch statement to process each item depending on if it is a user or group, and then replace the MemberName property depending on what it is: 我将使用switch语句根据每个项目是用户还是组来对其进行处理,然后根据其内容替换MemberName属性:

$Results = Switch(Get-ADGroupMember -Identity $GroupGUID){
    {$_.ObjectClass -eq 'User'} {$_ | Get-ADUser -Prop DisplayName | Select *,@{l='MemberName';e={$_.DisplayName}} -ExcludeProperty MemberName}
    {$_.ObjectClass -eq 'Group'} {$_ | Get-ADGroup | Select *,@{l='MemberName';e={$_.Name}} -ExcludeProperty MemberName}
}
$Results|Select MemberName,ObjectGUID

Or if you really want it all done in the pipeline you could do this: 或者,如果您真的希望在管道中完成所有操作,则可以执行以下操作:

Get-ADGroupMember -Identity $GroupGUID -PipelineVariable 'Member' | ForEach{If($Member.ObjectClass -eq 'User'){$_ | Get-ADUser -Properties DisplayName}Else{$_ | Get-ADGroup}} | Select @{l='MemberName';e={If($Member.ObjectClass -eq 'User'){$_.DisplayName}Else{$_.Name}}},ObjectGUID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将对象列表输出到带分隔符的单行 - How to output list of objects to a single line with separators Powershell - 组合 AD output 中的变量属性 - Powershell - Combining Variables Properties from AD output 如何在Powershell中扩展多个对象的属性并将输出导入CSV - How to expand properties for multiple objects in Powershell and import the output to CSV 如何使用 PowerShell 为 Azure-AD 设备对象添加扩展属性? - How to add Extension Properties for Azure-AD Device Objects using PowerShell? 将AD用户对象的属性列表提取到XML - Extract list of properties from AD user objects to XML 如何查看AD用户对象的所有属性? - How to view all properties of an AD User object? PowerShell - 如何将两个自定义对象的属性添加在一起以使用 - PowerShell - How to add the properties of two custom objects together to create a single merged custom object using 如何加快AD对象上的PowerShell操作 - How to speed up PowerShell operations on AD objects 无法更新本地主控目录同步对象的指定属性 - 更新 azure 广告中的用户管理器属性 - Unable to update the specified properties for on-premises mastered Directory Sync objects - Updating users manager attribute in azure ad 在Powershell中,如何在gridview中垂直列出AD计算机属性? - In powershell how can I list AD computer properties vertically in gridview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM