0

Im trying to cleanup old images in my ACR. It has 8 repositories so first I want it to test it in only one of them... The complicated thing about it that I need to keep last 4 images created. So I have this script:

$acrName = ACRttestt

$repo = az acr repository list --name $acrName --top 1

$repo | Convertfrom-json | Foreach-Object {
    $imageName = $_
    (az acr repository show-tags -n $acrName --repository $_ | 
       convertfrom-json |) Select-Object -SkipLast 4 | Foreach-Object {
           az acr repository delete -n $acrName --image "$imageName:$_"
       }
}

But Im receiving the following error:

Failed At line:9 char:58 + ... az acr repository delete -n $acrName --image "$imageName:$_" + ~~~~~~~~~~~ Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.

Any ideas?

Thanks in advance

AS Mackay
  • 2,831
  • 9
  • 19
  • 25
exitista
  • 563
  • 2
  • 10
  • 21

1 Answers1

0

You need to change the "$imageName:$_" into "${imageName}:$_". Then the script will like below:

$acrName = "ACRttestt"

$repo = az acr repository list --name $acrName --top 1

$repo | Convertfrom-json | Foreach-Object {
    $imageName = $_
    (az acr repository show-tags -n $acrName --repository $_ | 
       convertfrom-json |) Select-Object -SkipLast 4 | Foreach-Object {
           az acr repository delete -n $acrName --image "${imageName}:$_"
       }
}
Charles Xu
  • 29,862
  • 2
  • 22
  • 39