简体   繁体   English

将列表转置到Powershell中的矩阵

[英]Transpose list to matrix in powershell

I am trying to adapt some code I have found online and have the following: 我正在尝试改编我在网上找到的一些代码,这些代码具有以下内容:

$groups = Get-ADGroup -Filter {GroupCategory -eq ‘security’} -SearchBase ‘OU=Groups,DC=corp,DC=test,DC=local’
$users = Get-ADUser -Filter * -SearchBase “dc=corp,dc=test,dc=local”

$addADGroupMembers = @{}

foreach($group in $groups) {
    $addADGroupMembers.Add($group.Name, $group.ObjectGUID)
}

$htUsers = @{}
$htProps = @{}
$addADGroupMembers.Keys | foreach {$htProps.$_=$null}
foreach ($group in $addADGroupMembers.GetEnumerator()){
    foreach ($user in $group.Value){
        if (!$htUsers.ContainsKey($user)){
            $htProps.UserID = $user
            $htUsers.$user = $htProps.Clone()
        }
        ($htUsers.$user).$($group.Name) = 1
    }
}

This gives me a list of users, each with a list elements as below: 这给了我一个用户列表,每个用户都有一个列表元素,如下所示:

Accounts Administrators                                   : False
Support Read-Write                                        : False
Username                                                  : user1@test.local
Purchasing Read-Write                                     : False
Developers Administrators                                 : False
SharePoint 365 Administrators                             : False
Operations Read-Write                                     : False

Accounts Administrators                                   : True
Support Read-Write                                        : False
Username                                                  : joe@test.local
Purchasing Read-Write                                     : False
Developers Administrators                                 : False
SharePoint 365 Administrators                             : True
Operations Read-Write                                     : False

#snip...

But I have two things that i cant work out: 但是我有两件事我无法解决:

  1. How can I get the User Principal Name included? 如何获得包含的用户主体名称? I am unable to look this up from ObjectGUID and cant work out how to lookup from my users array. 我无法从ObjectGUID中查找此内容,也无法解决如何从用户数组中查找内容。

  2. How to sort this into a printed matrix, where I have rows of users down the side (and the UPN is the first column), and their associated groups (with a True in if they are a member) along the top. 如何将其分类为打印矩阵,其中用户排成一列(UPN为第一列),而其相关组(如果为成员则为True)位于顶部。

This would form the below: 这将形成以下形式:

                    Accounts        Support        Sales
User1@domain.com      1                             1
User2@domain.com                       1

Thanks for the help in advance, will issue some reputation points for the help. 预先感谢您的帮助,会为您的帮助发布一些声望点。

You don't want to use plain hashtables for this. 您不想为此使用普通哈希表。 Put UPN and group names in a (ordered) hashtable and create a custom object from that: 将UPN和组名放在一个(有序的)哈希表中,并从中创建一个自定义对象:

$groups = Get-ADGroup -Filter "GroupCategory -eq 'security'" -SearchBase 'OU=Groups,DC=corp,DC=test,DC=local'

Get-ADUser -Filter * -SearchBase "dc=corp,dc=test,dc=local" -Properties MemberOf | ForEach-Object {
  $props = [ordered]@{ 'Username' = $_.UserPrincipalName }
  $groups | ForEach-Object {
    $props[$_.Name] = $false
  }

  $_.MemberOf | Get-ADGroup | Select-Object -Expand Name | ForEach-Object {
    if ($props.ContainsKey($_)) {
      $props[$_] = $true
    }
  }

  New-Object -Type PSObject -Property $props
}

The output would look somewhat like this: 输出看起来像这样:

Username            Accounts        Support        Sales
--------            --------        -------        -----
User1@domain.com        True          False         True
User2@domain.com       False           True        False
...

If you want empty values instead of False for groups the user isn't a member of replace $false with the empty string ( '' ) and replace $true with whatever marker you want to use ( 1 , 'x' , ...). 如果你想为空值,而不是False团体用户无法取代的成员$false与空字符串( '' )和替换$true有你想使用的任何标记( 1'x' ,... )。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM