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.