Search code examples
azurepowershellazure-acr

Need tags from the images in aks


I have an images list like this

testcontainersgw.azurecr.io/reloader:v1.0.51
testcontainersgw.azurecr.io/tef/ip-id:19900909.1
testcontainersgw.azurecr.io/tef/ip-license:19900909.1
testcontainersgw.azurecr.io/tef/ip-finalsync:19900906.2
testcontainersgw.azurecr.io/tef/ip-tubelas:19900909.1
mcr.microsoft.com/aks/ip-masq-agent-v2:v0.1.11
mcr.microsoft.com/oss/kubernetes/kube-proxy:v1.30.3

and looking for an output with powershell command to only with the tags with specific name and discard remaining

ip-id
19900909.1
ip-license
19900906.2
ip-finalsync
19900906.2
ip-tubelas
19900909.1

Need output in same row

ip-id 19900909.1
ip-license 19900906.2
ip-finalsync 19900906.2
ip-tubelas 19900909.1

after the multiple values got list as below Input

ip-id 19900909.1
ip-license 19900906.2
ip-finalsync 19900906.2
ip-tubelas 19900909.1
ip-id 19900809.1
ip-license 19900905.2
ip-finalsync 19900606.2
ip-tubelas 19900709.1
ip-id 19900509.1
ip-license 19900406.2
ip-finalsync 19900306.2
ip-tubelas 19900309.1

Expecting as below

ip-id = "19900909.1","19900809.1", "19900509.1"
ip-license = "19900906.2", "19900905.2", "19900406.2"
ip-finalsync = "19900306.2", "19900906.2", "19900606.2"
ip-tubelas =  ".,.,."

Solution

  • The .Remove method in PowerShell doesn't work the way you're attempting to use it, as it expects a single string, and you're trying to use it on an array.

    To filter out the specific tags and names and discard the numbers at the beginning of the strings, you can use a combination of -replace to remove the numbers and Select-String or Where-Object to filter specific names.

    use this script

    # The list of image tags from the previous command
    $imageTags = @(
        "testcontainersgw.azurecr.io/reloader:v1.0.51",
        "testcontainersgw.azurecr.io/tef/ip-id:19900909.1",
        "testcontainersgw.azurecr.io/tef/ip-license:19900909.1",
        "testcontainersgw.azurecr.io/tef/ip-finalsync:19900906.2",
        "testcontainersgw.azurecr.io/tef/ip-tubelas:19900909.1",
        "mcr.microsoft.com/aks/ip-masq-agent-v2:v0.1.11",
        "mcr.microsoft.com/oss/kubernetes/kube-proxy:v1.30.3"
    )
    
    # Filter the image tags with "ip-" in the name and extract the tags
    $filteredTags = $imageTags | Where-Object {
        # Filter lines containing "ip-"
        $_ -like "*ip-*"
    } | ForEach-Object {
        # Extract the image name and version
        $nameVersion = $_ -split '[:/]'
        # Output the image name and version
        $nameVersion[2], $nameVersion[3]
    }
    
    # Output the filtered tags
    $filteredTags
    
    
    

    Example-

    Deploy your images

    enter image description here

    Check the pods

    enter image description here

    Run the script I shared above

    enter image description here

    update for your follow up question-

    To format the output of your PowerShell script so that the image name and version appear on the same line use the below script-

    edited script-

    # The list of image tags from the previous command
    $imageTags = @(
        "testcontainersgw.azurecr.io/reloader:v1.0.51",
        "testcontainersgw.azurecr.io/tef/ip-id:19900909.1",
        "testcontainersgw.azurecr.io/tef/ip-license:19900909.1",
        "testcontainersgw.azurecr.io/tef/ip-finalsync:19900906.2",
        "testcontainersgw.azurecr.io/tef/ip-tubelas:19900909.1",
        "mcr.microsoft.com/aks/ip-masq-agent-v2:v0.1.11",
        "mcr.microsoft.com/oss/kubernetes/kube-proxy:v1.30.3"
    )
    
    # Filter the image tags with "ip-" in the name and extract the tags
    $filteredTags = $imageTags | Where-Object {
        # Filter lines containing "ip-"
        $_ -like "*ip-*"
    } | ForEach-Object {
        # Extract the image name and version
        $nameVersion = $_ -split '[:/]'
        # Output the image name and version in one line
        "$($nameVersion[2]) $($nameVersion[3])"
    }
    
    # Output the formatted tags
    $filteredTags
    
    

    output using the previous script vs output using this edited one.

    enter image description here