Note that $cfgOffices is a hash table too, not an array.  If you use an array 
(round brackets instead of curly brackets), it will automatically be in the 
order you enter it.  Otherwise, there are a few options :


1.       If you are using PSv3, you can use the [Ordered] prefix to 
automatically have the items in the order you supply them (which already appear 
in alpha order below).  So it would look like:
$cfgOffices = [Ordered]@{
  "Albany" = $cfgAlbany
}


2.       You can sort the hash table you currently have using:

$cfgOffices.GetEnumerator() | Sort-Object Name

-Aakash Shah

From: [email protected] [mailto:[email protected]] On 
Behalf Of Danvers, Jim
Sent: Monday, June 23, 2014 8:59 AM
To: [email protected]
Subject: [NTSysADM] RE: Alright brainiacs - anyone good with powershell and 
sorting array's?

Right on - thanks.  See below:

#
# Create user accounts in AD, Exchange and Lync.
# This script will take input from the host and create user accounts based on 
that information.
#
# Requirements:
#  [+] Exchange Management Console installed.
#  [+] Lync Powershell module installed.

import-module lync;

$cfgTab = [char]9
$cfgCompany = "Big Company Inc";
$cfgMailDomain = "@bigco.com"; #E-Mail Domain

#=============================================================================
# A series of hash tables for office information.
#=============================================================================
$cfgAlbany = @{
  "OU" = "OU=Users,OU=Albany,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgAllentown = @{
  "OU" = "OU=Users,OU=Allentown,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgArizona = @{
  "OU" = "OU=Users,OU=Arizona,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgArkansas = @{
  "OU" = "OU=Users,OU=Arkansas,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgBinghamton = @{
  "OU" = "OU=Users,OU=Binghamton,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgBinghamtonTransportation = @{
  "OU" = "OU=Users,OU=Binghamton Transportation,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgBurlington = @{
  "OU" = "OU=Users,OU=Burlington,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgChesterfield = @{
  "OU" = "OU=Users,OU=Chesterfield,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgCincinnati = @{
  "OU" = "OU=Users,OU=Cincinnati,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgHatfield = @{
  "OU" = "OU=Users,OU=Hatfield,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgKentucky = @{
  "OU" = "OU=Users,OU=Kentucky,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgLaFargeville = @{
  "OU" = "OU=Users,OU=LaFargeville,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgLas Vegas = @{
  "OU" = "OU=Users,OU=Las Vegas,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgLevittown = @{
  "OU" = "OU=Users,OU=Levittown,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgLycoming = @{
  "OU" = "OU=Users,OU=Lycoming,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgOneida = @{
  "OU" = "OU=Users,OU=Oneida,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgPaterson = @{
  "OU" = "OU=Users,OU=Paterson,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgPhilly = @{
  "OU" = "OU=Users,OU=Philly,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgPlattsburgh = @{
  "OU" = "OU=Users,OU=Plattsburgh,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgPortland = @{
  "OU" = "OU=Users,OU=Portland,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgRemotes = @{
  "OU" = "OU=Users,OU=Remotes,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgSacramento = @{
  "OU" = "OU=Users,OU=Sacramento,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgSodus = @{
  "OU" = "OU=Users,OU=Sodus Corp,OU=Sodus,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgSuffield = @{
  "OU" = "OU=Users,OU=Suffield,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgSyracuse = @{
  "OU" = "OU=Users,OU=Syracuse,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgUniontown = @{
  "OU" = "OU=Users,OU=Uniontown,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgVernonPlant = @{
  "OU" = "OU=Users,OU=Vernon Plant,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgVernonTrans = @{
  "OU" = "OU=Users,OU=Vernon Trans,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgWestchester = @{
  "OU" = "OU=Users,OU=Westchester,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgWinchester = @{
  "OU" = "OU=Users,OU=Winchester,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgYork = @{
  "OU" = "OU=Users,OU=York,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };
$cfgYoungstown = @{
  "OU" = "OU=Users,OU=Youngstown,DC=bigco,DC=com";
  "DC" = "bigdaddydc1" };

#=============================================================================
# Creates an array of the above hash tables.
#=============================================================================
$cfgOffices = @{
  "Albany" = $cfgAlbany;
  "Allentown" = $cfgAllentown;
  "Arizona" = $cfgArizona;
  "Arkansas" = $cfgArkansas;
  "Binghamton" = $cfgBinghamton;
  "Binghamton Transportation" = $cfgBinghamtonTransportation;
  "Burlington" = $cfgBurlington;
  "Cincinnati" = $cfgCincinnati;
  "Chesterfield" = $cfgChesterfield;
  "Hatfield" = $cfgHatfield;
  "Kentucky" = $cfgKentucky;
  "LaFargeville" = $cfgLaFargeville;
  "Las Vegas" = $cfgLas Vegas;
  "Levittown" = $cfgLevittown;
  "Lycoming" = $cfgLycoming;
  "Oneida" = $cfgOneida;
  "Paterson" = $cfgPaterson;
  "Philly" = $cfgPhilly;
  "Plattsburgh" = $cfgPlattsburgh;
  "Portland" = $cfgPortland;
  "Remotes" = $cfgRemotes;
  "Sacramento" = $cfgSacramento;
  "Sodus" = $cfgSodus;
  "Suffield" = $cfgSuffield;
  "Syracuse" = $cfgSyracuse;
  "Uniontown" = $cfgUniontown;
  "Vernon Plant" = $cfgVernonPlant;
  "Vernon Trans" = $cfgVernonTrans;
  "Westchester" = $cfgWestchester;
  "Winchester" = $cfgWinchester;
  "York" = $cfgYork;
  "Youngstown" = $cfgYoungstown;
  };

#=============================================================================
# Gets a a list of mailbox databases and then chooses one based on day of year
#=============================================================================
# ... a bunch of exchange stuff here

#=============================================================================
# Checks AD for the user account.
#=============================================================================
# ... does the user already exist or not?

#=============================================================================
# Main processing
#=============================================================================
# ... more user creation stuff here

/////////////////////////////////////////
// *****************************
// *** This is where I'm stuck below  ***
// *****************************
/////////////////////////////////////////

                
#=============================================================================
                # Accept OU from host and verify it is in the array (and not 
NULL).
                
#=============================================================================
                $cfgOffices
        write-host " "
                Do {$strOffice = read-host "Choose OU from list above.  
Spelling counts, case does not!"
                                if (!$cfgOffices.Get_Item( $strOffice )) 
{write-host -Foreground Red "Invalid OU"}}
                While (!$cfgOffices.Get_Item ( $strOffice ))

                # $strOU              = $cfgOffices.Get_Item( $strOffice 
).Get_Item("OU")
                $strOU  = $cfgOffices.Get_Item( $strOffice ).Get_Item("OU")

I deleted a glob of stuff from here on down.  I'm pretty sure the above is 
where the script fetches the location strings ($cfgOffices - yes? ) and echo's 
them to the screen.  I'd like to have it sorted alphabetically on screen

-=- jd -=-


From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Damien Solodow
Sent: Monday, June 23, 2014 11:55 AM
To: [email protected]<mailto:[email protected]>
Subject: [NTSysADM] RE: Alright brainiacs - anyone good with powershell and 
sorting array's?

Should be fine, and I can take a look.
Might also check powershell.org as they have a lot of good helpers there too.

DAMIEN SOLODOW
Systems Engineer
317.447.6033 (office)
317.447.6014 (fax)
HARRISON COLLEGE

From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Danvers, Jim
Sent: Monday, June 23, 2014 11:52 AM
To: [email protected]<mailto:[email protected]>
Subject: [NTSysADM] Alright brainiacs - anyone good with powershell and sorting 
array's?

Is it cool to copy and paste script scrap in here?  I'm trying to get the 
output of an array sorted on screen so that it makes more sense when looking at 
it - I'm pretty sure I want to use "sot-object" but for the life of me can't 
figure out ~where~ to stick it.  I'm pretty sure it needs to be piped too ...  
?   ie ;  | sort-object

I have a snippet of code ready to send if anyone wants to take a look at it.  
It is some powershell code that the brain trust at the corporate hq came up 
with and they want us to use it.  It's functional but I'm just trying to clean 
it up some

-=- jd -=-

Reply via email to