0

I'm working on an issue with SCCM delivered App-V connection groups. Occasionally it delivers multiple (duplicate) connection groups to the client and the apps don't function correctly.

I'm running get-appvclientconnectiongroups in user context and where duplicates exist, exporting out Name, Groupid and Version id to a CSV.

I then import this using an elevated Powershell session (as I need admin rights to remove connection groups).

So CSV headers are

Name, GroupID, VersionID

The duplication lies in the Name header only

E.g.

Name, Group ID, Version ID

Adobe_Reader, 123, 456

Adobe_Reader, 456, 789

Adobe_Reader, 111, 555

Notepad, 333,222

Notepad, 111,444

Receiver, 444,777

Receiver, 123,999

What I would like to do is, for each duplicate name grab, the Group ID and Version ID to use in a remove-appvclientconnectiongroup. HOWEVER - I don't wish to do this for each entry - I want to stop when there is one left of each name (i.e when than name becomes unique in the list).

So in the end the list would be:

Adobe_Reader, 111, 555

Notepad, 111,444

Receiver, 123,999

And these are the ones we don't want to run throught the cmdlet

Any ideas? APologies if that makes no sense! I've been playing around with arrays but not getting anywhere fast.

2
  • One way or another you will need two arrays. See @Frode F. answer here Commented Jan 8, 2020 at 14:38
  • Not a solution but hopefully gets you in the right direction: $list = Import-Csv CSVFile.csv $app = "" $list | ForEach-Object { if ($_.Name -eq $app) { Write-Host "Remove " + $_.Name $_.'Group ID' } else { $app = $_.Name } } Commented Jan 8, 2020 at 14:44

3 Answers 3

1

Assuming you have a CSV file already, you can do the following to return the last item in a group of identical names:

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

Explanation:

Using Group-Object, you can group objects based on a property value. Here, grouping by property Name creates collection of items with properties Count,Name,Group. Name contains the values of the property you are grouping by. Count contains the number of matching values of that grouped property. Group contains the objects that had matching property values.

Since the Group property contains your objects, you can access the objects using the member access operator .. When piping to Foreach-Object, $_.Group will return the object groupings. Then you simply need to grab the last element [-1] of the collection.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have that information stored in a CSV file, you can do this to remove all but the last connection group:

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
}

Comments

0

Thanks! I managed to get it working with the code below after much messing about. As there can be multiple instances of duplicates I pushed everything into an editable array which a removes line from as the groups are removed. It then checks how many duplicates are left for any given package and stops when there's one of each left

$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 ```


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.