[英]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.