Search code examples
powershellazure-cli

Supressing AZ cli errors when calling az network vnet peering show


I have got the code below which will check to see if a network peering exists and act accordingly, I recently changed the bicep pipeline to add failOnStandardError: true as I noticed that sometimes the bicep code would fail but the pipeline gives the impression it ran successfully.

$peering = az network vnet peering show -g xxx -n enter-wrong-peering--vnet-name vnet_name --query "name" --output tsv

This returns the error.

ERROR: (NotFound) Resource /subscriptions/xx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/xxx/virtualNetworkPeerings/xxx not found. Code: NotFound Message: Resource /subscriptions/xxx/resourceGroups/xx/providers/Microsoft.Network/virtualNetworks/xxx/xxx not found.

My question is, is there a way to silence this section of the code below ?

      $ErrorActionPreference = "SilentlyContinue"
      try {

            $peering = az network vnet peering show -g xxx  -n enter-wrong-peering--vnet-name vnet_name --query "name" --output tsv
          }
      catch {
          Write-Output "Peering does not exist"
      }

I have tried to place the code in a try/catch block and setting the Errorcontrol to SilentlyContinue


Solution

  • Have you tried redirecting the stderr output?

    $peering = az network vnet peering show -g xxx  -n enter-wrong-peering--vnet-name vnet_name --query "name" --output tsv 2>error.txt