I’ve got a list of ADSiteNames and Subnets. My ideal output is:
Name: SiteName
Subnets: subneta
subnetb
subnetc
So that I could pipe this on to a further command and do somethning with each subnet. I can’t quite think of the formatting, so as I workaround I’ve come up with this, in which I add a SubNet$i noteproperty to the object with each iteration of a for loop. It works but is so very ugly, and working with the output later is dodgy at best.
If you have to do this task, how would you do it? I’d consider my solution to be very, very ugly, even if it works. I’m hoping you guys can show me a new construct or method of doing this.
Kind of hard when we don’t have the “source-data” of your $sites variable You could use the export-clixml cmdlet next time and add the xml from that as a file attatchment. That way we can import it and play with it easy.
If I understand you correctly, here is what I would do:
It sounds like what you want is an object that has a string property (site name), and an array property (Subnets). Anytime you find yourself creating properties or variables named Thingy1, Thingy2, etc, it’s a sure sign that you should actually be dealing with some sort of array or collection. You can also shorten the code quite a bit by making use of Group-Object, instead of reinventing that particular wheel yourself with the nested loop:
No problem! The -Object cmdlets are mostly pretty useful (though I don’t use Tee-Object much, for whatever reason.) If any of these look unfamiliar to you at this point, I’d suggest checking out their help files when you have some free time, and keep them in your toolbox. Heck, even if you know them, read the help files anyway; some of them have some sneaky features in parameters or syntax that you might not have known about.