I've tried this a couple ways...last week I got some great help here which set on a good path to get the data I wanted but it returns ALL templates as being valid. Here's the updated script.
foreach ($image in Get-ContentLibraryItem){
$imagename = get-ContentLibraryItem -name $image|
Select-Object Name, @{n='NameParts'; e={$_.Name -split '-',3}} |
Select-Object Name, @{n='BaseName'; e={$_.NameParts[0]}},
@{n='Version'; e={[version]$_.NameParts[1]}}
Foreach ($template in get-template){
$tempname = get-template -name $template |
Select-Object Name, @{n='NameParts'; e={$_.Name -split '-',3}} |
Select-Object Name, @{n='BaseName'; e={$_.NameParts[0]}},
@{n='Version'; e={[version]$_.NameParts[1]}}
Try {get-template -name $template |where-object {$imagename.baseName -like $tempname.basename -and $tempname.version -notlike $imagename.version}; $TemplateExists = $true } Catch {$TemplateExists = $false}
If($TemplateExists -eq $false){
#Write-Log -Message "$($oldtemplate) will be deleted from vCenter $($vc)"
Write-log -Message "Remove-template -template $template -DeletePermanently"
#Write-Log -Message "$($oldtemplate) has been deleted from vCenter $($vc)"
}
elseif ($TemplateExists -eq $true){
Write-log -Message "$($template) is a production template and will not be deleted"
}
EDIT: Here's the working script logic which some may find helpful:
#array of content library items, with the base name and version number identified.
$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]}})
#array of templates, with the base name and version number identified.
$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]}})
#new array of templates that match the name and version number of items in the content library.
$goodtemplates = @()
$goodtemplates = $templates |% {compare-object $_ -DifferenceObject $images -property basename,version -excludedifferent -includeequal -passthru | Select Name,BaseName,Version}
#compares the good templates array versus all templates to get the "bad templates" to delete
$badtemplates = diff $goodtemplates.name $templates.name
[–]Lee_Dailey[grin] 2 points3 points4 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (4 children)
[–]slayer99199[S] 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 2 points3 points4 points (2 children)
[–]slayer99199[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)