【问题标题】:Powershell - Parse duplicates in a listPowershell - 解析列表中的重复项
【发布时间】:2020-04-26 02:48:35
【问题描述】:

我正在处理 SCCM 交付的 App-V 连接组的问题。有时,它会向客户端提供多个(重复的)连接组,并且应用程序无法正常运行。

我在用户上下文中运行 get-appvclientconnectiongroups 并且存在重复项,将名称、组 ID 和版本 ID 导出到 CSV。

然后我使用提升的 Powershell 会话导入它(因为我需要管理员权限才能删除连接组)。

所以 CSV 标头是

名称、组 ID、版本 ID

重复只存在于名称标题中

例如

名称、组 ID、版本 ID

Adobe_Reader, 123, 456

Adobe_Reader, 456, 789

Adobe_Reader, 111, 555

记事本,333,222

记事本,111,444

接收者,444,777

接收者,123,999

我想做的是,对于每个重复的名称抓取,要在remove-appvclientconnectiongroup 中使用的组 ID 和版本 ID。但是 - 我不希望对每个条目都执行此操作 - 我想在每个名称左侧有一个时停止(即当名称在列表中变得唯一时)。

所以最终列表将是:

Adobe_Reader, 111, 555

记事本,111,444

接收者,123,999

这些是我们不想通过 cmdlet 运行的那些

有什么想法吗?道歉,如果这没有任何意义! 我一直在玩数组,但进展不快。

【问题讨论】:

  • 一种或另一种方式,您将需要两个数组。见@Frode F.answer here
  • 不是一个解决方案,但希望能让你朝着正确的方向前进:$list = Import-Csv CSVFile.csv $app = "" $list | ForEach-Object { if ($_.Name -eq $app) { Write-Host "Remove" + $_.Name $_.'Group ID' } else { $app = $_.Name } }

标签: arrays list powershell


【解决方案1】:

假设您已经有一个 CSV 文件,您可以执行以下操作以返回一组相同名称中的最后一项:

Import-Csv file.csv | Group-Object Name |
    Foreach-Object { $_.Group[-1] }

说明:

使用Group-Object,您可以根据属性值对对象进行分组。在这里,按属性Name 分组创建具有属性CountNameGroup 的项目集合。 Name 包含您作为分组依据的属性的值。 Count 包含该分组属性的匹配值的数量。 Group 包含具有匹配属性值的对象。

由于Group 属性包含您的对象,您可以使用成员访问运算符. 访问这些对象。当管道到Foreach-Object 时,$_.Group 将返回对象分组。然后你只需要抓住集合的最后一个元素[-1]

【讨论】:

    【解决方案2】:

    如果您将该信息存储在 CSV 文件中,则可以执行此操作以删除除最后一个连接组之外的所有连接组:

    Import-Csv -Path 'TheGroups.csv'  | Group-Object Name | Where-Object { $_.Count -gt 1 } | Foreach-Object { 
        $last = $_.Count - 2
        # remove all but the last connection group
        $_.Group[0..$last] | Remove-AppvClientConnectionGroup
    }
    

    【讨论】:

      【解决方案3】:

      谢谢!在搞砸了很多之后,我设法让它与下面的代码一起工作。由于可能有多个重复实例,我将所有内容推送到一个可编辑的数组中,当组被删除时,该数组中的行将被删除。然后它会检查任何给定的包还剩下多少个重复项,并在每个包都剩下一个时停止

      $data = import-csv $env:ALLUSERSPROFILE\AppvDuplciateGroups.csv
      
      #Push the data into an ArrayList so it can be edited on the fly
      
      $dataarray = [System.Collections.ArrayList]($data)
      
      #Get the array size, number of duplicates and target array size
      
      $arraysize = $dataarray.Count
      
      $dupescount = $dataarray| group name
      
      $arraytargetsize = $dupescount.count
      
      
      $i = $arraysize -1
      
      
      Function RemoveDuplicates(){
      
      #select the relevant duplicate from the array based in index number (running bottom to top)
      
      $lineX = $dataarray | select -Index $i
      
       #remove the duplicate
      
      Remove-AppvClientConnectionGroup -GroupId $lineX.groupid -VersionId $lineX.VersionId
      
       #remove enrty from the array
      
      $dataarray.RemoveAt($i)
      
       #check to see if that was the last entry for that particular connection group
      
      $getcount = $dataarray | group-object name| Select-Object name, count | where name -eq $lineX.name
      
       #if there's still more that one entry for that package, run the function again on the next line up
      
      If ($getcount.count -gt 1){
      
      $i = $i -1
      
      RemoveDuplicates}
      
       #if the array size is still larger than the calculated target array size, move up 2 lines in the array and run the function again
      
      Else{
      
          If ($dataarray.count -gt $arraytargetsize){
      
              $i = $i -2
      
              RemoveDuplicates}
      
       #once the array meets the calculated target size, repair all connection groups and remove .csv file          
      
                
                Else{
      
                      Repair-AppvClientConnectionGroup *
      
                      Remove-Item -Path $env:ALLUSERSPROFILE\AppvDuplicateGroups.csv}
      
      }
      
      }
      
      RemoveDuplicates ```
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-19
        • 2013-06-11
        • 2021-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-31
        相关资源
        最近更新 更多