So you have more than one disk. You’re only trying to put it in 1 Disk property. Do you want it to look like this?
Diskname = SomediskSomeotherdiskYetnotherdisk
Disksize = 100Gb200Gb300GB
That’s essentially what you are trying to do with your current code. So that disk’s name is “SomediskSomeotherdiskYetnotherdisk” and it’s size is 100Gb200Gb300GB. Probably not what you are wanting. If you know how many disks/what to expect and this information won’t change, you can account for it. For example, let’s say you will always have two disks.
[PSCustomObject]@{
VMName = $v.Name
ResourceGroupName = $v.ResourceGroupName
Interface = $n.name
Adaptor = $n.IpConfigurations.Name
PrivIP = $n.IpConfigurations.PrivateIpAddress
PublicIP = $n.IPConfigurations.PublicIpAddress
OStype = $v.StorageProfile.osDisk.osType
Disk1name = $Disk[0].name
Disk1Size = $Disk[0].DiskSizeGB
Disk2name = $Disk[1].name
Disk2Size = $Disk[1].DiskSizeGB
}
$report | Sort-Object PrivIP | Export-Csv -Path “c:\temp\AzureIP+Disk.Csv”
Now if you don’t know how many disks then you can run it through a loop.
$disks = @(
@{Name="OS";disksizeGB=100},
@{Name="DATA";disksizeGB=1000}
@{Name="BACKUP";disksizeGB=2000}
)
$object = [PSCustomObject]@{
VMName = "VMName"
ResourceGroupName = "ResourceGroupName"
Interface = "NetName"
Adaptor = "Name"
PrivIP = "PrivateIpAddress"
PublicIP = "PublicIpAddress"
OStype = "osType"
}
$count = 1
Foreach($d in ($disks | where {$_})){
$d.name
Add-member -InputObject $object -NotePropertyName "Disk$($count)Name" -NotePropertyValue $d.name
Add-member -InputObject $object -NotePropertyName "Disk$($count)Size" -NotePropertyValue $d.disksizeGB
$count++
}
$object | Sort-Object PrivIP | Export-Csv -Path “c:\temp\AzureIP+Disk.Csv” -NoTypeInformation
That will give you the CSV output you want.
"VMName","ResourceGroupName","Interface","Adaptor","PrivIP","PublicIP","OStype","Disk1Name","Disk1Size","Disk2Name","Disk2Size","Disk3Name","Disk3Size"
"VMName","ResourceGroupName","NetName","Name","PrivateIpAddress","PublicIpAddress","osType","OS","100","DATA","1000","BACKUP","2000"
Single property, single value.
Now, we need to discuss what I believe is a fundamental flaw in your code snippet. You are creating this custom object inside of
$report = foreach($n in $nic)
{
}
Which means you are going to get an object per NIC. Unless you are only always going to have 1 NIC per machine, but if so then why run it through a NIC loop?
I hope this gets you on the right track.