Tags


THAT’S a freaking mouthful!

Let’s put that into plain ordinary English.   An array is a matrix of values.  Most of the time you have a Single level Array (One Column is how I remember it).  Something like this.

$JelloFlavors=(“Fruity”,”Lemon”,”Mudpie”,”Poodle”,”Oranguntan”)

The other trick when creating these arrays is if the list is longer than the screen (which makes readability a pain).  You can also just break up the lines after each comma , like so.

$JelloFlavors=(“Fruity”,
”Lemon”,
”Mudpie”,
”Poodle”,
”Oranguntan”)

This is far more readable, probably easier to count visually

So to access any of these values you would reference the position in the Array.   Say #4 for “Poodle” flavoured Jello.  Remembering of course that your arrays start at 0.

$JelloFlavors[3]

Poodle

A MultiLevel Array array is actually multiple columns.   To create a MULTI level array you have to enclose each row within brackets ()

So a Multi-Leveled array would look like this.   The various sub levels can be Dynamic in size (Meaning they don’t all have to have the EXACT same amount of entries) which is part of the incredible Power in Windows Powershell

$Deserts=,(“Jello”,(“Fruity”,”Lemon”,”Mudpie”,”Poodle”,”Oranguntan”))
$Deserts+=,(“Pie”,(“Lemon”,”Apple”,”Chicken”))
$Deserts+=,((“Frog”,”Toad”),(“Crunchy”,”Raw”,”Surprise”,”Bones”))

If you were to try accessing members of THIS array keying in

$Deserts[1]

Would show you the list of “Pie” etc etc etc.   But since we have enclosed certain sets within Brackets they are treated as a SEPARATE array unto themselves

$Deserts[1][0]

Would show you “Pie”.  While if you accessed

$Deserts[1][1]

Would show you “Lemon”,”Apple” and “Chicken”.  But I can access the individual members of THAT array by specifying their position

$Deserts[1][1][2]

Which shows “Chicken”.

Confused?  If I want to access the “Toad” part in “Deserts” (since I’ve decided that for some reason Frog just wasn’t on the menu) I can key in

$Deserts[2][0][1]

Is you head spinning enough?  It get’s worse (or better depending on how you think) I can mix and match different types of variable IN these arrays.     But you might not do that.

The advantage to all of this is you can have a list of something where I am using this like Say Divisions in a company, And start matching up Security Groups by Division and Location.  Say I start with my organizational details like so.

$CompanyDetails=,(“New York”)
$CompanyDetails+=,(“Chicago”)
$CompanyDetails+=,(”WallaWalla”)
$CompanyDetails+=,(”Orlando”)

Now we can extend this to the Divisions from Each location.  Some may be larger than others.

$CompanyDetails=,(“New York”,(“Accounting”,”Management”,”AGotLost”))
$CompanyDetails+=,(“Chicago”,(“DataEntry”,”Accounting”,”HR”,”ChickenWings”))
$CompanyDetails+=,(”WallaWalla”,(“Us”,”Dem”))
$CompanyDetails+=,(”Orlando”,(“Mouseketeers”,”Mooseketeers”,”LegoEnforcement”))

Or some are a bit sillier

We could extend this that if a user was in a particular Company Location, they might have certain Security Groups to work as well.

$CompanyDetails=,(“New York”,
(“Accounting”,”Management”,”AGotLost”),
(“Tough Security”,”Really Tough”))

$CompanyDetails+=,(“Chicago”,
(“DataEntry”,”Accounting”,”HR”,”ChickenWings”),
(“Better than NY”,”Look out”,”Whoops Not This Group”,”Domain Admins”))

$CompanyDetails+=,(”WallaWalla”,
(“Us”,”Dem”),
(“Enterprise Admins”,”Fans of Powershell”))

$CompanyDetails+=,(”Orlando”,
(“Mouseketeers”,”Mooseketeers”,”LegoEnforcement”),
“ItsASmallWorld”,”SpaceMountain”,”WitchMountain”,”ThatMountain”))

So what you have now is an array that is 3 levels deep that you can easily build upon.   The TRICKY part in this equation is if you have to ADD to it.   So if I want to create a new entry within a specify point I have to reference the point directly and add to it. 

$CompanyDetails[2][1]+=,(“Another Stupid Security Group”)

Accessing that entry in the array will now have ANOTHER member

$CompanyDetails[2][1][2]

Another Stupid Security Group

Is this a perfect example? Probably not.  But hopefully it will give you a feel with how you can add and work with deeper arrays.   And yes.  If you go really over the top, you could build a really deep rabbit hole.

You are of course, best to store much of this information within an SQL database but sometimes we want to have the data localized to the Powershell script.

Remember, the Power of Shell is in YOU

Sean
The Energized Tech

Advertisements