从许可证阵列中删除重复项

时间:2013-11-19 02:25:39

标签: powershell

我在从数组中删除重复项时遇到问题。

$DataSheet = Import-Csv C:\Temp\Groups.CSV
$LicenseData = @()

foreach ($Group in $DataSheet) {
    $GroupMembership = Get-ADGroupMember $Group.Name | Select SamAccountName
    foreach ($Member in $GroupMembership) {
        $LicenseInfo = "" | Select "ID","GroupName","EngineLicense","UserLicense"
        $LicenseInfo.ID = $Member.SamAccountName
        $LicenseInfo.GroupName = $Group.Name
        $LicenseInfo.EngineLicense = $Group.EngineLicense
        $LicenseInfo.UserLicense = $Group.UserLicense
        $LicenseData += $LicenseInfo
    }
}

$LicenseData | Sort-Object ID |
    Export-Csv C:\Temp\LicenseData.CSV -NoTypeInformation -Force
$EngineLicense = $LicenseData | Group -Property $EngineLicense |
                 ForEach {$_.Group | Sort-Object EngineLicense -Unique} |
                 Select EngineLicense, ID |
                 Sort-Object EngineLicense
$UserLicense   = $LicenseData | Group -Property $UserLicense |
                 ForEach {$_.Group | Sort-Object UserLicense -Unique} |
                 Select UserLicense, ID |
                 Sort-Object UserLicense

这是$LicenseData看起来像

的示例
ID        GroupName EngineLicense UserLicense
--        --------- ------------- -----------
TestUser1 Group A   Mobile        Professional
TestUser1 Group B   ERQA          ERQA
TestUser1 Group B   ERQA          ERQA
TestUser2 Group A   Mobile        Professional
TestUser2 Group B   ERQA          ERQA

$EngineLicense看起来像

EngineLicense ID
------------- --
ERQA          TestUser1
Mobile        TestUser1

我要做的是显示每个用户的消费许可证,但删除用户的重复许可证。目前-Unique正在删除重复的引擎许可证。

理想情况下,这就是我想象的样子。请注意,它显示了两个用户使用的许可证,并删除了TestUser1的重复ERQA条目。

EngineLicense ID
------------- --
ERQA          TestUser1
Mobile        TestUser1
ERQA          TestUser2
Mobile        TestUser2

1 个答案:

答案 0 :(得分:3)

除非我误解了以下内容,否则会产生预期的结果:

$LicenseData | Select-Object -Unique EngineLicense, ID |
    Sort-Object ID, EngineLicense