Home > Blockchain >  PowerShell - Restructuring code to remove from group, then add to group before going to next group
PowerShell - Restructuring code to remove from group, then add to group before going to next group

Time:10-08

You will have to forgive me as I am defiantly beyond my capabilities here.

I have 2 lists, both containing a Group and then Skills. The first list are skills that need to be removed from the group, and the second is a list that have to be added back on.

Group Skill
Group1 Skill1
Group1 Skill2
Group2 Skill1
Group2 Skill2
... ...
Group15 Skill7

I am able to do this by pulling the list in (its in CSV format) and removing all the skills from the groups, and then re-adding them all.

foreach ($groups in $Profiles2Cleanse) {
<command called that actually performs this on a platform but removed for privacy>
Write-Output "$(Get-Date) - Removing - $($groups.Group) - $($groups.Skill)" #just for debug
}
foreach ($groups in $KnownGoodProfileList) {
<command called that actually performs this on a platform but removed for privacy>
Write-Output "$(Get-Date) - Adding - $($groups.Group) - to - $($groups.Skill)" #just for debug
}

What I want to be able to do is to go through each group and remove all the skills and then add them on - which is where I am stuck. So something along the lines of:

Remove all skills from Group 1 on Profiles2Cleanse list
Add all skill from Group 1 on KnownGoodProfileList
Remove all skills from Group 2 on Profiles2Cleanse list
Add all skill from Group 2 on KnownGoodProfileList
....
Remove all skills from Group 15 on Profiles2Cleanse list
Add all skill from Group 15 on KnownGoodProfileList

Any assistance would be greatly appreciated.

CodePudding user response:

You should be able to use Compare-Object to find differences and equalities between lists.

# The main list that is being updated
$master = Import-Csv master.csv

# Groups and Skills combinations that need to be removed from master.csv
$remove = Import-Csv remove.csv

# Groups and Skills combinations that need to be added to master.csv
$add = Import-Csv add.csv

# Remove unwanted groups/skills
# Finds only unique items from master list when compared to removed list
$master = Compare-Object -ReferenceObject $master -DifferenceObject $remove -Property Group,Skill |
    where SideIndicator -eq '<=' |
        Select Group,Skill

# Add new groups/skills
# Finds all items from master and add lists (no duplicates)
$master = Compare-Object -ReferenceObject $master -DifferenceObject $add -Property Group,Skill -IncludeEqual |
    Select Group,Skill

# Update master.csv
$master | Export-Csv master.csv -NoTypeInformation

CodePudding user response:

Much the same as the above poster I decided to take a similar approach to combine the lists with an extra field to denote the action.

#combine the CSVs into one list
$csv1 = Import-Csv $filePathRemove
$csv2 = Import-Csv $filePathAdd
$csv1  = $csv2
#export it because why not
$csv1 | Export-Csv $ExportCombined -NoTypeInformation
#import it again but sorted so it will remove skills and then add skills
$Profiles2Cleanse = Import-Csv $ExportCombined | sort @{expression='group';descending=$false}, action -Descending
foreach ($group in $Profiles2Cleanse){
    if($group.action -eq "remove") {
        Write-Output "$(Get-Date) - Removing - $($group.Group) - $($group.Skill)"
    }
    if($group.action -eq "add") {
        Write-Output "$(Get-Date) - Adding - $($group.Group) - to - $($group.Skill)"
    }
}
  • Related