Search code examples
azurepowershellcommand-line-interfaceazure-keyvaultazure-cloud-shell

Azure Cloud Shell Always Returns Null For $keyVault = Get-AzKeyVault -VaultName $keyVaultName -ResourceGroupName $rgName Command


I am running the below commands to get an instance of my KeyVault in my Azure Cloud Shell instance. But the same is always null.

$keyVaultName = "az203vmencryptdisk"
$rgName = "az203"
$vmName = "az203"
$keyVault = Get-AzKeyVault -VaultName $keyVaultName -ResourceGroupName $rgName
$diskEncryptionKeyVaultUrl = $keyVault.VaultUri

enter image description here

Because the value of $diskEncryptionKeyVaultUrl is null always, I am unable to proceed further. Anyone ever faced this issue?


Solution

  • I finally figured out the issue myself. I was having two subscription with my account. And I was aware that to run the Cloud Shell in Azure Portal, we need a related storage account, I was having such storage account in my other subscription.

    So my assumption is that Azure uses this storage account for all of my subscription to avoid multiple storage accounts for multiple subscription, for the same kind of metadata.

    The issue was that Azure Portal is not smart enough to find which subscription I am using currently, at least it may be a bug. So all I had to do is to set the context to my subscription where I have the KeyVault.

    Set-AzureRmContext -Subscription "subscriptionid"

    enter image description here

    Then I ran the preceding commands.

    $keyVaultName = "az203vmencryptdisk"
    $rgName = "az203"
    $vmName = "az203"
    $keyVault = Get-AzKeyVault -VaultName $keyVaultName -ResourceGroupName $rgName
    $diskEncryptionKeyVaultUrl = $keyVault.VaultUri
    

    I was able to get the output I was looking for.

    enter image description here

    Hope it helps.