I love this data structure!
 
I use it like this:
 
$hashtable = @{}   #  – this creates the table.. do it once
$hashtable[$key] = $value # – this populates the table.. do it once for each 
unique key. 
 
Then I iterate it like this:
Foreach ($key in $hashtable.keys) { <blah blah > }
 
So the question is: what are you wanting to use for the key (unique) and what 
will be the value the unique key points to?
 
So, $hashtable[$user.emailaddress] = $perm lets you associate one permission 
entry with a user’s unique email address. The problem here is each 
user.emailaddress will probably have multiple permission entries. I would be 
inclined to have the hashtable[key] point to an array list of permission 
entries. 
 
I’m sure there are other ways to slice this up.
 
Confession: Hash tables (dictionaries) are my favorite data structure; Array 
Lists are my second favorite.
 
Hth. Mike
 
From: [email protected] [mailto:[email protected]] On 
Behalf Of [email protected]
Sent: Wednesday, April 15, 2015 7:00 AM
To: [email protected]
Subject: [powershell] HashTable Help.
 
I really need to get my head around working with hashtables better.
 
Here is my snippet that I’m having problems with:
#Get list of mailboxes to work with.
$UserList = import-csv -Path "c:\temp\o365_termed_users.csv"
 
#Create Empty hashtable
$hashtable = $null
$hashtable = @{}
 
#Iterate through the list of user mailboxes
ForEach ($user in $userlist)
{
#Get the mailbox permissions, strip off the ones i dont care about.
$mailboxpermissions = Get-MailboxPermission –Identity $user.emailaddress | 
where-object {$_.User -notlike "*PRD*"} | where-object {$_.User -notlike "NT 
Auth*"} | Select-Object -Property User, AccessRights
    #Make sure we have permissions to work with.
    IF ($mailboxpermissions -ne $null)
    {
    #Iterate through the permissions, and add them to the hashtable.
    ForEach ($perm in $mailboxpermissions)
    {$hashtable.add($user.emailaddress,$perm.user,$perm.accessrights)}
    }
}
 
I’m having two issues.
1.      Adding the $user.emailaddress blows up with the overload error, I need 
to add it from the prior ForEach loop, so i know what mailbox has what 
permission.
2.      I’m not sure if my IF statement is setup properly, some of the 
mailboxes will return no permissions, and I know trying to add a null value to 
a hashtable won’t work correctly.
 
Sent from Windows Mail
 

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to