Quick PowerShell Stuff 12

In my lab setup script, one of the options for several functions is to work on a list of random mailboxes. For example, if I have 100 mailboxes and I select the apply to random mailboxes option,

Original Code
The first iteration (which I used in my first 3 versions of my lab setup script) only really randomized the number of mailboxes affected and not necessarily created a list of random mailboxes. The reason this was true was that although I selected a random number of mailboxes, the script only really pulled the from the beginning of the list. So if the randomized number was 10, the script would pull the first 10 mailboxes to used as the ‘random’ mailboxes. The script below is a bit modified from what is actually in the lab setup script, but it produces the same results – an un-randomized mailbox list.

$mbxcount = (get-mailbox | where {$_.RecipientTypeDetails -ne "DiscoveryMailbox"}).count
$range =  get-random -minimum 0 -maximum $mbxcount
$counter = 0
$mailbox = (get-mailbox | where {$_.RecipientTypeDetails -ne "DiscoveryMailbox"}).alias
$userlist = @()
while ($counter -lt $range) {
    $alias = $mailbox[$counter]
    $userlist += @($alias)
    $counter++
}

$userlist


After realizing the limitations of the function, I decided to re-write the function to make a truly random sample of mailboxes.

New Code – Truly Random
With a bit of work and testing, I’ve come up with a quick resolution to the issue what of is truly random AND unique. The unique part is guaranteed by lines 14-17. In these lines a search of previous stored values in the $userlist array which is keeping track of each mailbox selected. If the mailbox is found in the list, the $counter variable is reduced and the loop will restart again with the same $counter value. If the mailbox is not found in the array, it is added to the list and the counter increases in value:

function random-mailboxes {
    # Prep Random mailboxes (version 1)
    $mbxcount = (get-mailbox | where {($_.RecipientTypeDetails -ne "DiscoveryMailbox") -and ($_.RecipientTypeDetails -ne "RoomMailbox")}).count
    $range =  get-random -minimum 0 -maximum $mbxcount
    $mailbox = (get-mailbox | where {($_.RecipientTypeDetails -ne "DiscoveryMailbox") -and ($_.RecipientTypeDetails -ne "RoomMailbox")}).alias

    # Create Random User List
    $counter = 0
    $userlist = @()
    while ($counter -lt $range) {
        $position = get-random -minimum 0 -maximum $mbxcount
        $alias = $mailbox[$position]
        $validation = $false
            if ($userlist -contains $alias) {
                $validation = $true
                $counter--
            } 
        $counter++
        if ($validation -eq $false) {
            # Put all the mailboxes into a list of unique results
            $global:userlist += @($alias)
        }
    }
}

random-mailboxes
$userlist

Last note is line 21 which allows the list of mailboxes to be used in a different part of the script. The $global defines a variable that is not limited to just the called function. Lastly, I also added another criteria for excluding room mailboxes [lines 3 and 5].

Result Comparison
** Note in my test environment, there are only mailboxes that begin with the letter ‘a’

First Iteration – Not So Random

NotSoRandomMailboxes

Notice that the results are completely alphabetical.

Second Iteration – Very Random

RandomMailboxes

For these results, the names are no longer alphabetical and are completely random.

Look for the second iteration to be put into either version 1.4 or 1.5 of my lab setup script.


Resources
Operators
Global Variables

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