I want to have scripts only display certain properties if certain conditions are met. If a VM only has one disk for example, instead of ‘Disk 2’ showing as a property, but not showing any data, I don’t want ‘Disk 2’ to show up at all. Then of course if a VM has a “Disk 2”, it will show “Disk 2” in the report, and show data for it.
That’s going to be pretty hard to do and not really ideal. You are create a Key/Value pair and using this as a Property. The key is Disk2, when you call the object it will display all members of the object. You can select which properties to present via the Select-Object VM, Disk1, Disk2. However, if you are running this against multiple VM’s then this wouldn’t work because VM1 may have only 1 disk, but VM2 and VM3 have 2 disks. Instead you would be looking to create an array inside the hashtable instead. Please provide further code for review.
That is true, you can do this, but without having the full picture of the script, I don’t think this is effective. Based on the data that is provided it seems SB is trying to compile a report to be used. Only adding the property for select vms doesn’t make a lot of sense to me when we can just query the data and then filter based on Disk2 not equal to null i.e:
To me, it depends on whether you are trying to store these objects together or just spit them out as totally separate outputs. Most of the time, it makes more sense to output an array of objects, all with the same properties. That’s a lot more versatile, and it allows you to do things like piping the output to some other function.
So that would leave me with a couple of options:
Create all of the objects with same properties and just accept that some are going to have empty values for some VMs
Create all of the objects with a DiskDrives property that is an array of the disks
I would lean more towards #2, in this case, because a VM can have a (virtually) unlimited number of disks, so unless you know the max number of disks on each of your machines, there's no way to create a PSObject to handle that.
So in my example, if a VM doesn’t have any data disks, instead of that field just being blank for a particular VM when the report runs, I was wondering if the ‘Data Disk 0’, and subsequently “Data Disk 1”, and so on, if they could just not show up if a VM doesn’t have any data disks. I was just wondering if it were possible.
So for the report, there will always be a ResourceGroupName, and a VM name, but when it gets to Data Disk 0, it will only show for VMs that have that field.
Thanks, I’ll keep doing it the way that I have, if the VM doesn’t have any more than 1 disk, that property will just appear blank on the report. I was just curious if it was possible.