PowerShell and Arrays

I am currently working on a fairly long script and have decided that I need to ditch a bad habit I have with PowerShell. This bad habit is writing CSV files that contain the results of my looped queries. The reason I consider this a bad habit, is that it leaves behind files on whatever server I am running the script from. What I’ve decided to do is to venture into arrays within PowerShell for working with Exchange information. Now I have not completely given up CSV files as they are useful for creating HTML results pages or just a table of information, however, for values that I only need for part of a script I am going to try to use arrays as much as possible.

So what would you use an array for? Well, let’s take an example from a script I am working on now. Say you work in a large environment that has dozens or hundreds of Exchange Distribution Groups and you need to get a list of managers for each of these groups, then you need to add a manager to any group that has no manager.

** NOTE **
If your Exchange environment is a fresh install of Exchange 2010 you cannot create a group without a manager as one is assigned by default. The manager cannot be removed either by the EMC or via PowerShell. The only way to remove a manager in this case is via ADSIEdit. The value on the object in AD you need to change is ‘managedBy’. Delete whatever is there and replace it with ”. If however your environment was upgraded to Exchange 2010, then you will have groups with blank managers.

Now, back to arrays. The array I am using, if I have the terminology down, is a multidimensional array. This array stores data in a table like format. Let’s start with the first part of my script:

$MgrListArray =@()
$MgrListArray += ,@("DistributionGroup","Manager")
$mgr = get-distributiongroup -resultsize unlimited
foreach ($line in $mgr) {
if ($line.managedby -ne $null) {
$m = $line.managedby
$m | foreach {
$manager = $_.name}
} else {
set-distributiongroup -identity $line.name -managedby "Jerome Smith"
$manager = "Jerome Smith" }
$MgrListArray += ,@($line.name,$manager)
}

Line 1 is used to define my array ‘MgrListArray’.
Line 2 adds DistributionGroup and Manager as column headers. This is optional. Only use this if you are exporting to a CSV file as you will find this row to get in the way unless it contains actual data you need to manipulate.
Line 3 stores values from the get-distributiongroup command in a variable to be read in line 4
Line 4 begins a loop of reading the $mgr variable where I stored get-distributiongroup information. The $line variable is used to store each line of the variable $mgr for manipulation.
Line 5 is looking for any DL that has a manager defined. If the manager is defined, then it goes to line 6
Line 6 stores the entire managedBy attribute in a variable because managedBy is a multivalue attribute and this will make it easier to parse out all the details.
Line 7 Looks the new $m variable to parse out the information defined from Line 6.
Line 8 sets the $manager variable to match the name that was stored inside of $m.
Line 9 is the part 2 of the If Else loop. This part of the loop is for the DLs with blank managers.
Line 10 This sets the manager value for any distribution liat with a blank managedBy setting.
Line 11 sets $manager to the name used on the DL in line 10.
Line 12 ads the values for the array of distribution group and manager.
Line 13 is the end of the loop.

Now, why would I want to store all this information in an array? Well, let’s say in part two of your script you wanted to send an email to all list owners. To do this you would create a similar loop that would in this case use the array and email each DL owner to confirm their ownership:

foreach ($line in $mgrlistarray) {
if ($line[1] -ne "Jerome Smith") {
$smtp = get-mailbox $line[1] |select-object primarysmtpaddress
Send-MailMessage -to $smtp -from jsmith@testing.local -subject "Distribution Group Manager Confirmation $line[0]" -SmtpServer DS-2K10-EX01 -body "This email is being sent to you to confirm that you are the manager of this distribute group. Please let us know if you are not. Thanks."
} ELSE {
Send-MailMessage -to jsmith@Testing.Local -from jsmith@Testing.Local -subject "Distribution Group Manager Confirmation $line[0]" -SmtpServer DS-2K10-EX01 -body "This email is being sent to you to because this DL had a blank manager. Thanks."
}
}

This script runs very similarly to the first script. In this case we are reading the results of the array we created and sending an email to each owner to let them reply to confirm that they are a manager.


As you can see arrays are very helpful in transferring values from one set of code to another.


More Information

http://www.powershellpro.com/powershell-tutorial-introduction/variables-arrays-hashes/
http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/06/add-modify-verify-and-sort-your-powershell-array.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/09/easily-create-and-manipulate-an-array-of-arrays-in-powershell.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/24/weekend-scripter-an-insider-s-guide-to-powershell-arrays.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s