简体   繁体   English

如何在 PowerShell 中从 csv 查找 Active Directory 组名称?

[英]How to look for Active Directory group name from csv in PowerShell?

If I have a .csv :如果我有一个.csv

ClientCode,GroupCode
1234,ABC
1234,DEF
1235,ABC 
1236,ABC

and I want to get a hashtable with ClientCode as key, and values to be all AD groups with ClientCode in it, for example:我想用一个哈希表ClientCode为重点,和值是所有AD组与ClientCode在里面,例如:

ClientCode GroupCode      
---------- ---------      
1234       ClientGroup_InAD_1234, some_other_client_1234    
1235       ClientGroup_InAD_1235, some_other_client_in_AD_1235
1236       ClientGroup_InAD_1236

How do I go about this?我该怎么做?

Essentially, I have client groups in Active Directory and each client has a code which is the same as the 'ClientCode' in the csv.本质上,我在 Active Directory 中有客户端组,每个客户端都有一个与 csv 中的“ClientCode”相同的代码。 For example, I might have a client called 'Bob' which I have assigned a code '1234' to it.例如,我可能有一个名为“Bob”的客户端,我为它分配了一个代码“1234”。 Therefore, the Group for Bob in the AD would be 'Bob_1234'.因此,AD 中 Bob 的组将是“Bob_1234”。 Essentially I want to be able to search for whatever groups have ClientCode in them.本质上,我希望能够搜索其中包含 ClientCode 的任何组。 So i want to search for the all the AD groups that have '1234'.所以我想搜索所有具有“1234”的 AD 组。 This would return 'Bob_1234' and whatever group in the AD also has '1234' in its name.这将返回“Bob_1234”,并且 AD 中的任何组的名称中也包含“1234”。

So far I have tried:到目前为止,我已经尝试过:

$clientTable = @{}

foreach($rec in $csv_data) {
    $groups = @(get-adgroup -Filter "name -like '*$($rec.clientcode)_*'")
    write-host "Found $($groups.count) group(s) for: $($rec.clientcode)"
    $clientTable[$ClientCode] = @($groups)
}

$clientTable

but I'm not getting my desired output但我没有得到我想要的输出

If you want a hash table populated with the ClientCode value as the key name, you can do the following:如果您希望使用ClientCode值作为键名填充哈希表,您可以执行以下操作:

$clientTable = @{}

foreach($rec in $csv_data){
    $groups = @(Get-ADGroup -Filter "Name -like '*_$($rec.ClientCode)'" | Select -Expand Name)
    write-host "Found $($groups.count) group(s) for: $($rec.ClientCode)"
    $clientTable[$rec.ClientCode] = $groups

}

$clientTable

Keep in mind here that each value in the hash table is an array of group names.请记住,哈希表中的每个值都是一个组名数组。 If you want a single string with comma-delimited names, you can do $clientTable[$rec.ClientCode] = $groups -join "," instead.如果您想要一个带有逗号分隔名称的字符串,您可以执行$clientTable[$rec.ClientCode] = $groups -join ","

You can use the loop like this.您可以像这样使用循环。 You will need to search with a * at the beginning of the name you are looking to find via the Filter.您需要在要通过过滤器查找的名称的开头使用 * 进行搜索。

foreach($rec in $csv) { 
    $clientCode = "*_$($rec.ClientCode)"
    if (!($clientTable.ContainsKey($clientCode))) {
        $names = Get-ADGroup -Filter 'Name -like $clientCode' | select Name
        $clientTable[$clientCode] = $names -join ","
    }
}

This will also check for any client IDs that have already been checked and ignore those.这还将检查任何已经检查过的客户端 ID 并忽略它们。

You will need to de-duplicate the ClientCodes in the CSV before retrieving the groups.在检索组之前,您需要对 CSV 中的 ClientCode 进行重复数据删除。 Something like below should do it (assuming the ClientCode is always preceded by an underscore like in _1234 as shown in your examples)应该像下面这样(假设 ClientCode 总是前面有一个下划线,如_1234中所示,如您的示例所示)

$csv = Import-Csv -Path 'ClientGroupCodes.csv'

$clientTable = @{}

$csv | Select-Object -ExpandProperty ClientCode -Unique | ForEach-Object {
    # $_ is a single clientcode in each iteration (string)

    # get an array of goup names
    $groups = @(Get-ADGroup -Filter "Name -like '*_$_'" | Select-Object -ExpandProperty Name)
    Write-Host "Found $($groups.Count) group(s) for code : '$_'"
    $clientTable[$_] = $groups -join ', '
}

$clientTable

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

相关问题 使用来自csv的powershell删除活动目录samaccount名称中的空白区域 - remove white space in active directory samaccount name with powershell from csv Powershell导出包含活动目录安全组数据的csv文件 - Powershell exports csv file containing active directory security group data 如何从Powershell中使用多个变量查找Active Directory? - How do I look up Active Directory from powershell with multiple variables? 如何从Active Directory获取组名和EmployeeID? - How to get group name and EmployeeID from Active Directory? 通过Powershell使用导入的.csv从Active Directory中提取名字和姓氏 - Extract First and Last Name from Active Directory using imported .csv through Powershell 添加名称中带有空格的Active Directory组会在PowerShell脚本中给出错误 - Adding Active Directory group with spaces in name gives error in PowerShell script 使用 Powershell,如何获取 Active Directory 组成员列表,包括用户和联系人,显示姓名、公司和 email - Using Powershell, how to get a list of Active Directory group members, including users and contacts, showing name, company, and email 如何在活动目录中搜索组名? - how to search for a group name within the active directory? Active Directory组成员-Powershell - Active Directory Group Members - Powershell 使用 Powershell 添加 Active Directory 组 - Adding an Active Directory group with Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM