簡體   English   中英

對來自 Active Directory 的數據進行分組

[英]Grouping data from Active Directory

想知道我是否可以獲得有關我的 powershell 腳本的幫助。 我已經走到了一半(甚至可能是那里的 3/4),但我只是在努力讓我的群組在每個用戶的一行中分組....

不好解釋

現在我可以從多個 AD 組中獲取所有用戶,花了一些時間,但我最終到達了那里......

但是它顯示 CSV 是這樣的:

first_name,last_name,email,group_list
John,Smith,JSmith@email.com,Group1
John,Smith,JSmith@email.com,Group2
Emily,Rogers,ERogers@email.com,Group1
Emily,Rogers,ERogers@email.com,Group3

雖然沒關系,但我真的很想像這樣格式化數據:

first_name,last_name,email,group_list
John,Smith,JSmith@email.com,Group1~Group2
Emily,Rogers,ERogers@email.com,Group1~Group3

到目前為止,這是我的代碼

## Define the groups, This includes a wildcard which gets all users in groups with that pattern
$Groups = (Get-AdGroup -filter * | Where {$_.name -like "GroupName*"} | select name -expandproperty name) 

## Var for array, empty
$Array = @()

## Var for data
$Data = [ordered]@{
}


## For Each loop to get members of each group
Foreach ($Group in $Groups)
{
## Define the search criteria for AD Search
$Members = Get-ADGroupMember -identity $Group | Get-ADUser -Properties * | select  givenName,sn,sAMAccountName,mail
foreach ($Member in $Members)
{
$Data."first_name" = $Member.givenName
$Data."last_name" = $Member.sn
$Data."email" = $Member.mail
$Data."group_list" = $Group

## Store in PSObject
$DataPSObject = New-Object PSObject -property $Data

## Add to array so it is no longer empty
$Array += $DataPSObject

}

}


## Export array into CSV 
$Array | export-csv "C:\temp\DataFromArray.csv" -NoTypeInformation

由於電子郵件是唯一標識符,我嘗試在電子郵件屬性上對對象進行分組,但輸出對我沒有用

## Export array into CSV 
$Array | Group-Object -Property email | export-csv "C:\temp\DataFromArray.csv" -NoTypeInformation

我還嘗試使用定義的分隔符加入組 -join '~' 但這似乎只是創建了一長串加入的組(當我這樣說時是有道理的)

希望有人有一些想法嗎?

謝謝

只需快速獲得您想要的東西:

## Define the groups, This includes a wildcard which gets all users in groups with that pattern
$Groups = Get-AdGroup -Filter 'Name -like "GroupName*"' | Select-Object -ExpandProperty Name

## For Each loop to get members of each group
$Array = foreach ($Group in $Groups) {
    ## Define the search criteria for AD Search and capture in variable $Array
    Get-ADGroupMember -Identity $Group | 
        Get-ADUser -Properties GivenName,Surname,SamAccountName,EmailAddress | 
        Select-Object @{Name = 'first_name'; Expression = {$_.GivenName}},
                      @{Name = 'last_name'; Expression = {$_.Surname}},
                      @{Name = 'email'; Expression = {$_.EmailAddress}},
                      @{Name = 'GroupName'; Expression = {$Group}}

}

$out = $Array | Group-Object email | ForEach-Object {
    # join the GroupName property of this user to get a delimited string
    $grouplist = $_.Group.GroupName -join '; '
    # output a new object with the 'group_list' property
    # this will create duplicates objects, so we need Select-Object * -Unique at the end
    $_.Group | Select-Object first_name, last_name, email, @{Name = 'group_list'; Expression = {$grouplist}}
} | Select-Object * -Unique

$out | Export-Csv "C:\temp\DataFromArray.csv" -NoTypeInformation

希望有幫助

您需要對Group-Object的輸出做更多的處理,但您已經快完成了!

$Array |Group-Object -Property email |ForEach-Object {
    [pscustomobject]@{
        first_name = $_.Group[0].first_name
        last_name  = $_.Group[0].last_name
        email      = $_.Group[0].email
        groups     = $_.Group.group_list -join '~' # join all the group names together
    }
} |Export-Csv "C:\temp\DataFromArray.csv" -NoTypeInformation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM