Make sure $subkey has a value as well:
foreach( $subkey in $hashtable.$key.Keys )
I would just test the variables to make sure you get values back for each. Just
loop through each one and check the output.
foreach( $key in $hashtable.Keys ) { $key }
#and
foreach( $subkey in $hashtable.$key.Keys ) { $subkey }
From: [email protected] [mailto:[email protected]] On
Behalf Of [email protected]
Sent: Wednesday, April 15, 2015 3:37 PM
To: [email protected]
Subject: Re: [powershell] HashTable Help.
If i write-host $hashtable.keys
I get a space delimited list of the email addresses.
Sent from Windows Mail
From: Kelley, Matthew<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 3:19 PM
To: [email protected]<mailto:[email protected]>
What do you have in $hashtable before you call this line? The error implies you
are trying to loop through all the keys in the $hashtable variable, but
probably didn’t set $hashtable to anything.
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of
[email protected]<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 2:43 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [powershell] HashTable Help.
You cannot call a method on a null-valued expression.
At line:1 char:143
+ $a = $(foreach( $key in $hashtable.Keys ) { $str = $key.ToString() +
‘.’;foreach( $subkey in $hashtable.$key.Keys ) { $str += $subkey.ToString <<<<
() + ‘,’ } $str })
+ CategoryInfo : InvalidOperation: (ToString:String) [],
RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Sent from Windows Mail
From: Michael B. Smith<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 12:37 PM
To: [email protected]<mailto:[email protected]>
This is a little more complicated since you have embedded objects.
$a = $( foreach( $key in $hashtable.Keys ) { $str = $key.ToString() + ‘.’;
foreach( $subkey in $hashtable.$key.Keys ) { $str += $subkey.ToString() + ‘,’ }
$str }
$a should contain pretty much what you want.
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of
[email protected]<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 11:48 AM
To: [email protected]<mailto:[email protected]>
Subject: Re: [powershell] HashTable Help.
Almost everything i need, when i look at the hashtable, it does not have the
emailaddress present.
Other than that, it works great 😉
$UserList = import-csv -Path "c:\temp\o365_termed_users.csv"
$hashtable = $null
$hashtable = @{}
ForEach ($user in $userlist)
{
$mailboxpermissions = Get-MailboxPermission –Identity $user.emailaddress |
where-object {$_.User -notlike "*PRD*"} | where-object {$_.User -notlike "NT
Auth*"} | Select-Object -Property Identity, User, AccessRights
If( $null –ne $mailboxPermissions )
{
$hashTable.$($user.emailaddress) = $mailboxPermissions
}
}
$hashtable | Select-object -expand Values | export-csv -Path
"c:\temp\0365_termed_user_mailbox_permissions_assigned.csv" -NoTypeInformation
Sent from Windows Mail
From: Michael B. Smith<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 11:18 AM
To: [email protected]<mailto:[email protected]>
You have
$mailboxpermissions = Get-MailboxPermission –Identity $user.emailaddress |
where-object {$_.User -notlike "*PRD*"} | where-object {$_.User -notlike "NT
Auth*"} | Select-Object -Property User, AccessRights
Which provides you a single collection already. Just store it. ☺
If( $null –ne $mailboxPermissions )
{
$hashTable.$($user.emailaddress) = $mailboxPermissions
}
You can iterate through the permissions collection as you generate your report.
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of
[email protected]<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 11:03 AM
To: [email protected]<mailto:[email protected]>
Subject: Re: [powershell] HashTable Help.
Here is the output from get-mailboxpermission (its a test user)
PS C:\Windows\system32> Get-MailboxPermission –Identity
"[email protected]<mailto:[email protected]>" | Select-Object
-Property User, AccessRights
User
AccessRights
----
------------
NT AUTHORITY\SELF
{FullAccess, ReadPermission}
NAMPRD05\Administrator
{FullAccess}
NAMPRD05\Domain Admins
{FullAccess}
NAMPRD05\Enterprise Admins
{FullAccess}
NAMPRD05\Organization Management
{FullAccess}
NT AUTHORITY\SYSTEM
{FullAccess}
NT AUTHORITY\NETWORK SERVICE
{ReadPermission}
PRDMGT01\View-Only Organization Management
{ReadPermission}
NAMPRD05\Administrator
{FullAccess, DeleteItem,
ReadPermission, ChangePermission, ChangeOwner}
NAMPRD05\Domain Admins
{FullAccess, DeleteItem,
ReadPermission, ChangePermission, ChangeOwner}
NAMPRD05\Enterprise Admins
{FullAccess, DeleteItem,
ReadPermission, ChangePermission, ChangeOwner}
NAMPRD05\Organization Management
{FullAccess, DeleteItem,
ReadPermission, ChangePermission, ChangeOwner}
NAMPRD05\Public Folder Management
{ReadPermission}
NAMPRD05\Exchange Servers
{FullAccess, ReadPermission}
NAMPRD05\Exchange Trusted Subsystem
{FullAccess, DeleteItem,
ReadPermission, ChangePermission, ChangeOwner}
NAMPRD05\Managed Availability Servers
{ReadPermission}
I need to have the mailbox added to the output, since I am building a report to
generate a list of permissions to be removed from multiple mailboxes.
Ideas on best method?
Sent from Windows Mail
From: Mike Mitchell<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 10:47 AM
To: [email protected]<mailto:[email protected]>
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]>
[mailto:[email protected]] On Behalf Of
[email protected]<mailto:[email protected]>
Sent: Wednesday, April 15, 2015 7:00 AM
To: [email protected]<mailto:[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.
1. 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
================================================
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
================================================
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
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be
used for urgent or sensitive issues
================================================
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
**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be
used for urgent or sensitive issues
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1