Script: Merge Multiple Arrays

Posted by : on

Category : howto   project   vmware   esx   powershell   script


Each Saturday my company does a refresh of a few different machines. This refresh consists of cloning a couple of production machines into our lab environment for development. It’s a very tedious process that takes the entire day to do. Until recently, this process has been done manually. I’ve been working on a script to automate the process as much as possible. Along the way, I’ve learned a great deal about powershell/powercli - especially from hitting a wall and trying to figure out a way around. This blog entry is about one of those snags along the path of progress.

The refresh consists of two major Acts. Act I is all about cloning the source VMs, stripping them down, and converting them into templates. Act II is about deploying VMs using differing templates. There will be a later entry discussing this entire script in greater detail. This entry focuses on creating merged array to be used in the deployment CSV list for Act II.

Each week, the refresh list changes. One week, the source VMs may be SourceVM01 & SourceVM05. Next week, it may be SourceVM01 & SourceVM03. I created a GUI frontend for this script using PrimalForms 2011. The GUI has a “pick-list” where you can cherry pick what SourceVMs and what DestinationVMs will be needed. Upon your selection, three arrays are built - $a = #VM Names, $b = #Specs, $c = #Template Names.

For example; If I needed to refresh DEV-VM01, TEST-VM01, and QA-VM01; I’d check their boxes, and the following variables would be added to the three arrays. (You can start to see where if you had multiple Dev and Test VMs how these arrays can get quite complex.)

DEV-VM1 TST-VM1 QA-VM1
$avm = “DEV-VM1” $avm = “TST-VM1” $avm = “QA-VM1”
$bspec = “DEV” $bspec = “TST” $bspec = “QA”
$ctmp = “TMPL-DEV01” $ctmp = “TMPL-TST01” $ctmp = “TMPL-QA01”

So the question was, how do you take this information and merge it - with one array, and all the information within? That’s where I came up with this solution. I replay each of the arrays using a “counter” ($i) and building a new array with the information from the table above. The counter variable matches each record for that row (ie. $a.record1 with $b.record1 and $c.record1).

$d=@()
for($i=0;$i -lt $a.count;$i++)
	{
	$row = "" | Select-Object VMName, Spec, Template
	$row.VMName = "$($a[$i])"
	$row.Spec = "$($b[$i])" 
	$row.Template = "$($c[$i])"
	$d+=$row
	} 

The array will continue to loop, until it runs out of entries based on the $a array. Once this fourth array ($d) has been created, I can then use it to build my deployment CSV file (SEE THE DEPLOYMENT CSV LIST BUILD SCRIPT - To come).


About Sam Aaron
Sam Aaron

Father, Husband, Geek. Workaholic.

Email : mail@micronauts.us

Website : http://micronauts.us

About Sam Aaron

Father. Husband. Geek. Workaholic. US Marine Corps Veteran.

Sam Aaron is a Senior Consultant in the Professional Services Organization for Entelligence, bringing over a decade of expertise in enterprise cloud automation and infrastructure. Sam has spent almost eleven years at VMware leading cloud automation initiatives using VCF Automation (formerly Aria Automation & vRA) and designing scalable, multi-tenant environments with VMware Cloud Director (vCD).

Sam holds multiple certifications including VCF-Architect 2024, VCIX-CMA, and dual VCPs (DCV & CMA), and is a recognized contributor to VMware’s certification exams. As a VMware Hands-On Lab (HOL) Captain and content author from 2015-2025, Sam played a key role in educating and mentoring the global VMware community. He helped to create and develop the automation challenge and troubleshooting labs for VMworld and global virtual forums.

When Sam is not working, he has several hobbies, among these are 3D printing Star Wars robots and turning them into animatronics.

Launched in April 2010, micronauts is Sam's online presence. Here, he has been blogging and sharing knowledge with the virtualization community. This blog acts as a central repository to retain the resolutions and other trivial knowledge that Sam has discovered.

** No information provided here was reviewed or endorsed by VMware by Broadcom, Microsoft, or anyone else for that matter. All information here are opinions based on Sam's personal experience. Use this knowledge at your own risk. **

Star
Useful Links