0

I am trying to create a report to find all the storage account and its associated vnet details.

 & {
foreach ($storageAccount in Get-AzStorageAccount) {
$storageAccountName = $storageAccount.StorageAccountName
$resourceGroupName = $storageAccount.ResourceGroupName  
 
 $context = (Get-AzStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName $resourceGroupName).NetworkRuleSet.VirtualNetworkRules.VirtualNetworkResourceId
 
 $splitarray = $context.Split('/')
 $vnetid = $splitarray[8]
 $subscriptionid = $splitarray[2]
 
   New-Object psobject -Property @{
       Name = $storageAccountName;
       Context = $vnetid;
       ResourceGroupName = $resourceGroupName
       Subscriptionid = $subscriptionid
   }

 }
} | Format-Table Name, Context, subscriptionid, ResourceGroupName

I am currently getting the below output: storage account vnet report

As you can see from the output the vnet name is not properly fetched for the storage accounts.

Storage account testfnapp2oct16 has vnet testfnvnet attached, this is correct. Storage accounts unz2versvaultea, cs1f7b27d61e31e, win10guestdiag954 doesn't have any vnet attached but 'testfnvnet' is repeated until the value changes for a different storage account.

Storage account testfnapp9eb7 has two vnets but only testvnet1 is shown and the value 'testvnet1' is repeated for next storage account.

Any help is much appreciated.

Sridevi
  • 10,599
  • 1
  • 4
  • 17
Cbcpsrsh
  • 1
  • 1
  • At first glance, you have typos: the output object defines property `Context`, but later you use it as `vnetid`. `Subscriptionod` should be `Subscriptionid`. Then, since you are not showing us the full string you have in `$context`, it is impossible for us to see if indeed it has at least 9 parts after splitting on the slash, or if it has some value at all.. – Theo Oct 19 '22 at 13:23
  • Thank you Theo, for your inputs. My bad, I have fixed the typos now and updated my original question with current status of report. On your question, it has 9 parts and now as given in the script , the subscriptionid and vnet is correctly fetched. But there seems to be a problem with loop. Could you please check again. – Cbcpsrsh Oct 19 '22 at 15:29
  • Did the issue resolved?? – Imran Nov 12 '22 at 16:22

1 Answers1

0

I tried to reproduce the same in my environment it's work me successfully.

I have created sample storage account fetched with vnet and without attached with vnet when I tried, your code I got the same error as below.

enter image description here

To resolve this issue, Please utilize the condition I have changed in the code below to execute this issue's solution properly.

& {
foreach ($storageAccount in Get-AzStorageAccount) {
$storageAccountName = $storageAccount.StorageAccountName
$resourceGroupName = $storageAccount.ResourceGroupName
$context = (Get-AzStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName $resourceGroupName).NetworkRuleSet.VirtualNetworkRules.VirtualNetworkResourceId
$ErrorActionPreference = ‘SilentlyContinue’
      if ($context -eq $null){
      New-Object psobject -Property @{
       Name = $storageAccountName;
       Context = "Empty" ;
       ResourceGroupName = $resourceGroupName
       Subscriptionid =  $subscriptionid
   }
      }
      else {
         $splitarray = $context.Split('/')
$vnetid = $splitarray[8]
$subscriptionid = $splitarray[2]
  New-Object psobject -Property @{
       Name = $storageAccountName;
       Context = $vnetid;
       ResourceGroupName = $resourceGroupName
       Subscriptionid = $subscriptionid
   }
}
}
} | Format-Table Name, Context, subscriptionid, ResourceGroupName

Result:

Now, whenever vnet are not fetched in my storage account it's show Empty like below.

enter image description here

Asati
  • 17
  • 4
Imran
  • 3,875
  • 2
  • 3
  • 12