1

I am trying to get the list of storages name for a specific subscription using powershell. If I have the correct subscription name, I get the result but if there is some mistype then I get this error:

Select-AzSubscription : Please provide a valid tenant or a valid subscription. Select-AzSubscription -SubscriptionName $subName CategoryInfo : CloseError: (:) [Set-AzContext], ArgumentException FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand

What would be the best option to handle the error message and exit out from the script with a message "Couldn't find the subscription". Here is the powershell code:

###Set a subscription name
$subName = "Test SubscriptionName"

Select-AzSubscription -SubscriptionName $subName

### Select storage accounts based for above subscription
$sAccount = Get-AzStorageAccount | select StorageAccountName
$sAccount

3 Answers 3

1

The better solution should be use the scripts below:

$subName = "Test SubscriptionName"

try{ 
     Select-AzSubscription -SubscriptionName $subName -ErrorAction Stop        
    } 
catch [System.ArgumentException]{
        Write-Host "Couldn't find the subscription"

        #use exit to exit the script
        exit
     }

#Select storage accounts based for above subscription      
$sAccount = Get-AzStorageAccount | select StorageAccountName
$sAccount
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, makes sense! That way, I can see where the error is.
1

You can just do a null test:

$sub = Select-AzSubscription -SubscriptionName $subName -ErrorAction SilentlyContinue
if ($sub -eq $null) 
  { 
    "Couldn't find the subscription"
    ## Add you exit code here!
  }  
else
  { "Good subname" }

Comments

1

I was able to solve it using try-catch method. Here is the solution:

 $subName = "Test SubscriptionName"

Try { 
     Select-AzSubscription -SubscriptionName $subName -ErrorAction Stop
        #Select storage accounts based for above subscription
         $sAccount = Get-AzStorageAccount | select StorageAccountName
          $sAccount
         } 
Catch{
        Write-Host  $_.Exception.Message
     }

4 Comments

It's not a good solution. If the subname is correct, and the error is from the following code like Get-AzStorageAccount, your code still prints the message "couldn't find the subscription".
@IvanYang how about now? is it possible to add a custom error message with the Exception message ? For example: if the error is in subName then 'Please re-enter your subName' + Exception message and if it is in Get-AzStorageAccount then 'You don't have storage account' + Exception?
yes, you can add a custom error message. And you can also specify the exception type as mentioned in my answer.
@IvanYang, just saw your answer. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.