0

I am writing a query to capture azure virtual machine disk details and saving it in array. This VM has two disks. Unfortunately my array is overwritten by the second disk. i.e., Details of the second disk is appearing twice instead of each lines for each disks.

$t = @()
$array =@()
foreach ($disk in $r.storageProfile.DataDisks.Name) {
    $t= Get-AzDisk -ResourceGroupName $ResourceGroupName -DiskName $disk
    $ReportDetails.VMName =  $r.Name 
    $ReportDetails.DiskName =  $t.Name  
    $ReportDetails.DiskSizeGB=  $t.DiskSizeGB
    $ReportDetails.sku=  $t.Sku.Name  
    $ReportDetails.Lun=  ($r.storageProfile.DataDisks | Where-Object -filterscript {$_.Name -eq $disk}).lun
    $ReportDetails.Caching=  ($r.storageProfile.DataDisks | Where-Object -filterscript {$_.Name -eq $disk}).Caching
    $ReportDetails.DiskIOPSReadWrite=   $t.DiskIOPSReadWrite  
    $ReportDetails.DiskMBpsReadWrite =  $t.DiskMBpsReadWrite
    $ReportDetails 

    $array += $ReportDetails  
}
$array | Out-GridView

When I print the value of $ReportDetails. it appears properly with correct data during two iterations.

However array() is showing second disk twice.

4
  • 3
    What is $ReportDetails ? Commented Dec 23, 2022 at 1:54
  • 1
    As the question from Santiago implies, $ReportDetails, is probably an object where each property references to a value (which you overwrite with each iteration). As a general best practice, I recommend you to avoid using the increase assignment operator (+=) to create a collection and use the pipeline to build the $array as lined out in the referal. Commented Dec 23, 2022 at 8:13
  • ReportDetails is a class object which is ByRef (not ByValue) so there is only one instance of the object. You would need to use new-object to create more than one instance. Commented Dec 23, 2022 at 10:05
  • The question could use more detail. Commented Dec 25, 2022 at 14:39

1 Answer 1

1

Not at all sure what your variable $ReportDetails is like, but I think you mean to get an array of objects with chosen properties about each disk.

Something like this:

$array = foreach ($diskName in $r.storageProfile.DataDisks.Name) {
    $azDisk   = Get-AzDisk -ResourceGroupName $ResourceGroupName -DiskName $diskName
    $dataDisk = $r.storageProfile.DataDisks | Where-Object {$_.Name -eq $diskName}
    [PsCustomObject]@{
        VMName            = $r.Name
        DiskName          = $azDisk.Name
        DiskSizeGB        = $azDisk.DiskSizeGB
        sku               = $azDisk.Sku.Name
        Lun               = $dataDisk.lun
        Caching           = $dataDisk.Caching
        DiskIOPSReadWrite = $azDisk.DiskIOPSReadWrite
        DiskMBpsReadWrite = $azDisk.DiskMBpsReadWrite
    }
}
$array | Out-GridView
Sign up to request clarification or add additional context in comments.

Comments

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.