I've been battling this for a couple weeks. Basically, I want to compare data in 2 multidimensional arrays.
Here's my first array:
$images = @(Get-ContentLibraryItem |
Select-Object Name, @{n='NameParts'; e={$_.Name -split '-',3}} |
Select-Object Name, @{n='BaseName'; e={$_.NameParts[0]}},
@{n='Version'; e={[version]$_.NameParts[1]}})
Output looks like this:
Name BaseName Version
---- -------- -------
sles11sp4_jeos-1234567890 sles11sp4_jeos
sles12sp3-0.0.11 sles12sp3 0.0.11
win2012r2std_desk-0.1.23 win2012r2std_desk 0.1.23
win2012r2std_desk-0.2.34 win2012r2std_desk 0.2.34
win2016std_desk-0.3.45 win2016std_desk 0.3.45
win2016std_desk-0.4.56 win2016std_desk 0.3.33
Here's my 2nd array:
$templates = @(get-template |
Select-Object Name, @{n='NameParts'; e={$_.Name -split '-',3}} |
Select-Object Name, @{n='BaseName'; e={$_.NameParts[0]}},
@{n='Version'; e={[version]$_.NameParts[1]}})
And the output looks like this:
Name BaseName Version
---- -------- -------
sles12sp3-0.0.11-infra-dr01 sles12sp3 0.0.11
win2016std_desk-0.3.33-infra-dr01 win2016std_desk 0.3.33
win2016std_desk-0.3.42-infra-dr01 win2016std_desk 0.3.42
win2012r2std_desk-0.1.23-infra-dr01 win2012r2std_desk 0.1.23
win2012r2std_desk-0.2.22-infra-dr01 win2012r2std_desk 0.2.22
sles12sp3-0.0.31-infra-dr01 sles12sp3 0.0.31
win2016std_desk-0.3.45-infra-dr01 win2016std_desk 0.3.45
win2012r2std_desk-0.2.34-infra-dr01 win2012r2std_desk 0.2.34
sles11sp4_jeos-1234567890-infra-dr01 sles11sp4_jeos
sles12sp3-0.0.11-oracle01 sles12sp3 0.0.11
sles12sp3-0.0.31-oracle01 sles12sp3 0.0.31
sles11sp4_jeos-1234567890-oracle01 sles11sp4_jeos
sles12sp3-0.0.11-stnd-linux01 sles12sp3 0.0.11
win2016std_desk-0.3.33-stnd-win01 win2016std_desk 0.3.33
win2016std_desk-0.3.33-stnd-sql01 win2016std_desk 0.3.33
win2016std_desk-0.4.56-stnd-win01 win2016std_desk 0.4.56
win2012r2std_desk-0.1.23-stnd-win01 win2012r2std_desk 0.1.23
sles12sp3-0.1.22-stnd-linux01 sles12sp3 0.1.22
win2016std_desk-0.3.45-stnd-sql01 win2016std_desk 0.3.45
win2012r2std_desk-0.2.22-stnd-win01 win2012r2std_desk 0.2.22
sles12sp3-0.1.33-stnd-linux01 sles12sp3 0.1.33
win2016std_desk-0.3.42-stnd-win01 win2016std_desk 0.3.42
At the end of the day I'm looking for all $templates where $template.basename -eq $image.basename and $template.version -eq $image.version and delete everything else. The problem I'm encountering is that if I compare them one at a time in foreach loops, they will eventually delete all the templates. How do I compare the arrays together so I can either get all the "good" templates or all the "bad" templates in a new array.
This is the last iteration of my code (which didn't work...didn't delete anything).
Foreach ($image in $images){
Foreach ($template in $templates){
if ($template |where-object {$_.basename -eq $image.basename -and $_.version -eq $image.version}){
Write-host "Template Name Matches, next"
}
Else {
Write-host "Image and version do not match, deleting"
Write-log -Message "Remove-template -template $($template.name) -DeletePermanently"
}
}
}
Here's the final code that works! NOTE: I need to add a switch command to export the deleted to a csv, if the switch is true, it will execute the delete.
$images = @(Get-ContentLibraryItem |
Select-Object Name, @{n='NameParts'; e={$_.Name -split '-',3}} |
Select-Object Name, @{n='BaseName'; e={$_.NameParts[0]}},
@{n='Version'; e={[version]$_.NameParts[1]}})
$templates = @(get-template |
Select-Object Name, @{n='NameParts'; e={$_.Name -split '-',3}} |
Select-Object Name, @{n='BaseName'; e={$_.NameParts[0]}},
@{n='Version'; e={[version]$_.NameParts[1]}})
$goodtemplates = @()
$goodtemplates = $templates |% {compare-object $_ -DifferenceObject $images -property basename,version -excludedifferent -includeequal -passthru | Select Name,BaseName,Version}
$badtemplates = diff $goodtemplates.name $templates.name
Foreach ($badtemplate in $badtemplates){
Write-host "Image and version do not match, deleting"
Remove-template -template $($badtemplate.inputobject) -DeletePermanently -confirm:$false
}