Powershell 从CSV创建映射以获取广告用户
我对PowerShell是新手。我正在尝试从我的CSV创建一个映射,将广告组中的用户列表到一个哈希表中。 我的csv如下所示:Powershell 从CSV创建映射以获取广告用户,powershell,csv,active-directory,Powershell,Csv,Active Directory,我对PowerShell是新手。我正在尝试从我的CSV创建一个映射,将广告组中的用户列表到一个哈希表中。 我的csv如下所示: ClientCode,GroupCode 1234,ABC 1234,DEF 1235,ABC GroupCode是Active Directory组的代码 例如: 组首字母缩写“ABC”对应一个名为“group_One”的广告组 组码“DEF”映射到名为“group_Two”的广告组 组码“GHI”映射到一个名为“group_Three”的广告组 每个广告组都有用户
ClientCode,GroupCode
1234,ABC
1234,DEF
1235,ABC
GroupCode是Active Directory组的代码
例如:
组首字母缩写“ABC”对应一个名为“group_One”的广告组
组码“DEF”映射到名为“group_Two”的广告组
组码“GHI”映射到一个名为“group_Three”的广告组
每个广告组都有用户。
例如:
广告“第一组”中的用户:约翰·多伊、杰西卡·西蒙斯
广告“第二组”的用户:卡特·麦卡锡、杰里米·L
广告“第三组”的用户:艾莉森·卡特、埃里克·克罗斯、亚历杭德拉·菲舍尔
本质上,我想创建某种映射或一种方法,将CSV中的“GroupCode”链接到广告组,然后在哈希表中列出其用户
预期的产出将是:
ClientCode GroupUsers
---------- ---------
1234 John Doe, Jessica Simmons, Carter McCarthy, Jeremy L
1235 John Doe, Jessica Simmons
1236 Alyson Carter, Eric Cross, Alejandra Fischer
为了帮助您开始,您可以将GroupCode添加到AD Group对象的notes字段中。然后
get-adgroup Group1 -Properties info
你可以访问这些数据
我看到你或某人在这里发布了相同的问题,似乎有人建议:
您可以创建一个包含代码到组名映射的查找哈希表,并使用该哈希表检索所有用户名,如下所示:
# create a mapping hash for the group codes to the real group names
$groupsMap = @{
'ABC' = 'Group_One'
'DEF' = 'Group_Two'
'GHI' = 'Group_Three'
#etc.
}
$csv = Import-Csv -Path 'ClientGroupCodes.csv'
# loop though the csv items, group by property ClientCode
$result = $csv | Group-Object ClientCode | ForEach-Object {
# get an array of the various group codes inside each client group
$groupCodes = $_.Group.GroupCode | Select-Object -Unique
# loop through these codes, find the real names from the hash
# and collect the user names in variable $groupUsers
$groupUsers = New Object 'System.Collections.Generic.List[string]'
foreach ($code in $groupCodes) {
# test if the group code can be found in the hash
if ($groupsMap.ContainsKey($code)) {
$users = [string[]](Get-ADGroupMember -Identity $groupsMap[$code] | Where-Object { $_.objectClass -eq 'user' }).Name
$groupUsers.AddRange($users)
}
else {
Write-Warning "No mapping found for group code $code"
}
}
# output an object with the properties combined
[PsCustomObject]@{
'ClientCode' = $_.Name
'GroupUsers' = ($groupUsers.ToArray() | Sort-Object -Unique) -join ', '
}
}
# output on screen
$result
# output to CSV file
$result | Export-Csv -Path 'GroupUsersPerClient.csv' -NoTypeInformation
这可能是学校的作业吗?看起来与@Theo中的非常相似。不,我编辑了这个问题,使其更具意义。这很有帮助,是否仍有删除重复项的方法?@ineedhelp你能告诉我重复项在哪里吗?使用
-Unique
选择组码和每个组的用户。我将为组用户获取重复的组码。我认为这与用户在多个组中有关,我只想在散列中已经存在的情况下添加一次table@ineedhelp我已经更新了代码以使用一个收集用户名的列表对象。这样可以避免获取数组数组,从而导致组用户的名称重复。如果我想要一个哈希表,其中clientcode为键,值为AD中名称中包含clientcode的组,该怎么办?我应该更新这个问题吗?