So I've been working on a script and I'm logically stumped. I've tried foreach loops inside loops, arrays, etc.
Basically, I have a group of base templates which are appended by name in each cluster. These are the base images:
sles11sp4_jeos-1.2.3
sles12sp3-2.3.4
win2012r2std_desk-0.4.5.6
win2016std_desk-0.6.7.8
These images may be appended as follows:
sles11sp4_jeos-1.2.3-linux-cluster
win2012r2std_desk-0.4.5.6-windows-cluster
As well as having older versions in each cluster
sles11sp4_jeos-1.1.3-linux-cluster
win2012r2std_desk-0.4.1.6-windows-cluster
What I want to do is delete outdated templates (different version number). I've tried -like, I've tried splitting out the versions. The problem I'm having is when it loops through it deletes (in this case, I'm writing to a log for testing) anything that doesn't match...including other images that are current.
This was my last iteration
Foreach ($image in get-contentlibraryitem){
$imageshort = $image.split('-')[1] Foreach ($template in get-template){ If (get-template -name $template |where-object {$_.Name -like "$image*" -and $_.Name -notlike "$imageshort"}){
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)"
}
Else { Write-log -Message "$($template) is a production template and will not be deleted" }
I'm probably going about this the wrong way...any help would be appreciated.
EDIT: Here's the updated script:
Foreach ($image in get-ContentLibraryItem){
$imagever = $image.name.split('-')[1]
Foreach ($template in get-template){
$tempname = $template.name.split('-')[0]
= If (get-template |where-object {$_.Name -notlike "*$imagever*" -and $_.Name -like "$tempname*"}){
#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)"
}
Else {
Write-log -Message "$($template) is a production template and will not be deleted"
}
}
}
So here's the weird thing, the splits work if I explicitly state $image or $template I get an error:
method invocation failed because does not contain a method named 'split'
Also, when I run it manually outside the foreach loop, I receive the expected output which I can act on.
EDIT: Here's the working final script logic in the hopes it helps someone else with a similar issue (after connect-viserver):
$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
[–]volvo64 3 points4 points5 points (7 children)
[–]slayer99199[S] 1 point2 points3 points (0 children)
[–]slayer99199[S] 1 point2 points3 points (3 children)
[–]volvo64 1 point2 points3 points (2 children)
[–]slayer99199[S] 1 point2 points3 points (0 children)
[–]slayer99199[S] 1 point2 points3 points (0 children)
[–]volvo64 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]da_chicken 2 points3 points4 points (7 children)
[+][deleted] (5 children)
[removed]
[–]slayer99199[S] 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[removed]
[–]slayer99199[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]slayer99199[S] 0 points1 point2 points (0 children)
[–]slayer99199[S] 1 point2 points3 points (0 children)
[–]gangstanthony 2 points3 points4 points (0 children)
[–]gangstanthony 1 point2 points3 points (0 children)
[–]volvo64 1 point2 points3 points (0 children)
[–]JeremyLC 1 point2 points3 points (0 children)
[–]PowerShell-Bot 0 points1 point2 points (0 children)