本文介绍了如何在PowerShell中从csv查找Active Directory组名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有 .csv

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

,我想获取一个以 ClientCode 作为键的哈希表,并将值设为所有带有 ClientCode ,例如:

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 GroupCode
---------- ---------
1234       ClientGroup_InAD_1234, some_other_client_1234
1235       ClientGroup_InAD_1235, some_other_client_in_AD_1235
1236       ClientGroup_InAD_1236

我该怎么办?

本质上,我在Active Directory中有客户端组,每个客户端都有一个代码,与csv中的 ClientCode相同。例如,我可能有一个名为鲍勃的客户,我为其分配了代码 1234。因此,广告中Bob的组为 Bob_1234。本质上,我希望能够搜索其中包含ClientCode的任何组。因此,我想搜索所有具有 1234的广告组。这将返回 Bob_1234,并且广告中的任何组名称中也都有 1234。

Essentially, I have client groups in Active Directory and each client has a code which is the same as the 'ClientCode' in the csv. For example, I might have a client called 'Bob' which I have assigned a code '1234' to it. Therefore, the Group for Bob in the AD would be 'Bob_1234'. Essentially I want to be able to search for whatever groups have ClientCode in them. So i want to search for the all the AD groups that have '1234'. This would return 'Bob_1234' and whatever group in the AD also has '1234' in its name.

到目前为止,我已经尝试过:

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

推荐答案

您可以使用循环像这样。您需要在要通过过滤器查找的名称的开头以*开头进行搜索。

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 ","
    }
}

这还将检查是否已检查所有客户端ID,并忽略它们。

This will also check for any client IDs that have already been checked and ignore those.

这篇关于如何在PowerShell中从csv查找Active Directory组名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:11