1

Get-AzSnapshot is showing some weird behaviour.

Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName

First Issue: The command above gives an error :

Resource group 'xxx' could not be found. ErrorCode: ResourceGroupNotFound ErrorMessage: Resource group
     | 'xxx' could not be found. ErrorTarget:  StatusCode: 404 ReasonPhrase: Not Found 

but there is a command to list the resourcegroup after and before this command and it executes correctly.

Apparently the issue is it cannot find the snapshot but instead it is blaming RG.

Second Issue. Snapshot listed on portal is not listed when just Get-AzSnapshot is executed. This is not the issue with being in correct subscription because other command like az group list list from all subscriptions. More over the subscription was forcefully loaded with az account set --subscription c56e18b5–xxx

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
Blue Clouds
  • 7,295
  • 4
  • 71
  • 112

1 Answers1

0

First Issue

I can only reproduce your error using Get-AzSnapshot when the resource group is invalid or not found, which will return ErrorCode: ResourceGroupNotFound. If you supplied a correct resource group, but an invalid snapshot name, you will instead get ErrorCode: ResourceNotFound. I would double check the resource group you are trying to access definitely exists.

If you want to make sure you don't run Get-AzSnapshot if the resource group doesn't exist, I would add a error variable with -ErrorVariable to store the error result if an exception occured from Get-AzResourceGroup. The error result is stored as type System.Collections.ArrayList, so we can simply check if the errors found is greater than zero. You can run $resourceGroupError.GetType().FullName to get the full type name. We can also use -ErrorAction SilentlyContinue to suppress the error from Get-AzResourceGroup if the resource group is not found.

$resourceGroupName = "demo-RG"
$snapshotName = "demo-snapshot"

# First get resource group
Get-AzResourceGroup -Name $resourceGroupName -ErrorVariable resourceGroupError -ErrorAction SilentlyContinue

if ($resourceGroupError.Count -gt 0)
{
    # ResourceGroup doesn't exist
    # Don't get snapshot
    Write-Host "Cannot find $resourceGroupName resource group"
}
else
{
    # ResourceGroup exist
    # Can get snapshot
    Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
}

Second Issue

Only resource groups under your current active subscription will be listed if you run az group list or Get-AzResourceGroup. So if your snapshot is in a specific subscription, and you are not currently set to that subscription, you need to switch to that specific subscription with az account set --subscription. For Azure PowerShell, I suggest switching contexts to your active subscription using this method.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75