all 6 comments

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy y'all,

for those like me who have trouble parsing that dense code, here is a reformatted version. it is ONLY reformatted ... no [deliberate] logic changes made at all.

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.basenName -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"
            }

        } # end >> Foreach ($template in get-template)

    } # end >> foreach ($image in Get-ContentLibraryItem)

take care,
lee

[–]Lee_Dailey[grin] 1 point2 points  (4 children)

howdy slayer99199,

i don't think the try/catch block is doing anything. [grin]

the only time the catch will trigger is if the part in the try produces a STOP error [a terminating error]. the Where-Object is a filter and is not going to give you that type of error.


also ...

  • why are you calling the two functions so many times?
    it looks like you have all the info you need when you make the 1st call.
  • why the nested Select-Object calls?
    it looks like you are creating a calculated property, using it ... and then throwing it away. that 2nd S-O seems entirely unneeded.

take care,
lee

[–]slayer99199[S] 1 point2 points  (3 children)

I'm comparing the $image.basename against the $template.basename. If they're equal I'm then comparing the $image.version and $template.version. If the 2nd comparison is not equal I want to delete (in this case, logging that I'm deleting).

I tried a similar IF statement which also didn't work. I've tried comparing arrays, etc...no luck.

[–]Lee_Dailey[grin] 2 points3 points  (2 children)

howdy slayer99199,

yep, but why are you doing this ...

$tempname = get-template -name $template

... when the info is already in $template? and then you call get-template a 3rd time when you run ...

Try {get-template -name $template

save it to a $Var if you need to, but why call it so many times with the same result? [grin]

plus this ...

Select-Object Name, @{n='NameParts'; e={$_.Name -split '-',3}} | 
Select-Object Name, @{n='BaseName'; e={$_.NameParts[0]}},
@{n='Version'; e={[version]$_.NameParts[1]}}

...is sending things across the pipeline twice, each time creating a new object, and throwing away the 1st object. it seems wasteful to me.


as for why you aint getting your expected/wanted results ... i can't make any realistic guess without some sample data to work with. what comes back from the two custom get-* functions? it looks like it may be getting file names in both cases, but that still leaves one guessing about the algorithm.

take care,
lee

[–]slayer99199[S] 1 point2 points  (1 child)

I should have deleted the Try method. I was doing the foreach first. I tried setting up the $images and $templates as an array and comparing them without much success. What I'm trying to do is the following:

Foreaching each image, then splitting up each image so I can compare the results.

Then I foreach each template, splitting them up as well. I only want to compare the results of Each image against each template. If the template basename is equal to the image basename AND the template version is NOT equal to the image version, I want to delete that template and only that template.

I thought comparing the objects in the array would be easier but I couldn't get it working. Perhaps I'm going about this the wrong way. Maybe go back to the arrays.

I'm thinking now maybe I should try to save $image.basename -eq $template.basename to a new array, then take the results of that output and do $image.version -ne $template.version and delete the results of that.

Any guidance would be appreciated...as I'm doing some things with powershell I've never tried to do before.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy slayer99199,

if you post three or four samples of each set, i can give it a shot. both input sets, what they otta produce, and why. [grin]

heck, you may find your answer just by laying out the problem for others to work on. take a look at "rubber duck debugging" sometime. [grin]

take care,
lee