Powershell Dynamic Menu Based on Mailbox Permission Entries
up vote
1
down vote
favorite
I'm having trouble with creating a dynamic menu from mailbox permission entries in PowerShell. The below function gets a list of permission entries for the selected mailbox and outputs a table.
Function AuditSingleMailboxPermission($mbox) {
If ($mbox -like $null){
Write-Host "Please select the mailbox from the list below"
$mbox = SelectMailbox
}
$mboxPermissions = Get-MailboxPermission $mbox | where {$_.User -like "*@*"}
Return $mboxPermissions
}
The next function uses the output of the previous to build the menu.
Function SelectMailboxPermission($mbox) {
$rights = (AuditSingleMailboxPermission -mbox $mbox).User
Write-Host $rights
$menu = @{}
for ($i=1;$i -le $rights.count; $i++) {
Write-Host "$i. $($rights[$i-1])"
$menu.Add($i,($rights[$i-1]))
}
[int]$ans = Read-Host "Enter selection"
$selection = $menu.Item($ans)
Return $selection
}
The Write-Host $rights line shows the full username. However, the menu only returns the first letter of the username. This is a problem because the output of this function is used to remove user permissions on a mailbox. I can't figure this out for the life of me.
powershell
add a comment |
up vote
1
down vote
favorite
I'm having trouble with creating a dynamic menu from mailbox permission entries in PowerShell. The below function gets a list of permission entries for the selected mailbox and outputs a table.
Function AuditSingleMailboxPermission($mbox) {
If ($mbox -like $null){
Write-Host "Please select the mailbox from the list below"
$mbox = SelectMailbox
}
$mboxPermissions = Get-MailboxPermission $mbox | where {$_.User -like "*@*"}
Return $mboxPermissions
}
The next function uses the output of the previous to build the menu.
Function SelectMailboxPermission($mbox) {
$rights = (AuditSingleMailboxPermission -mbox $mbox).User
Write-Host $rights
$menu = @{}
for ($i=1;$i -le $rights.count; $i++) {
Write-Host "$i. $($rights[$i-1])"
$menu.Add($i,($rights[$i-1]))
}
[int]$ans = Read-Host "Enter selection"
$selection = $menu.Item($ans)
Return $selection
}
The Write-Host $rights line shows the full username. However, the menu only returns the first letter of the username. This is a problem because the output of this function is used to remove user permissions on a mailbox. I can't figure this out for the life of me.
powershell
Try$menu.Add($i,$($rights[$i-1]))
. I think you forgot the dollar sign there
– Theo
Nov 22 at 14:11
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm having trouble with creating a dynamic menu from mailbox permission entries in PowerShell. The below function gets a list of permission entries for the selected mailbox and outputs a table.
Function AuditSingleMailboxPermission($mbox) {
If ($mbox -like $null){
Write-Host "Please select the mailbox from the list below"
$mbox = SelectMailbox
}
$mboxPermissions = Get-MailboxPermission $mbox | where {$_.User -like "*@*"}
Return $mboxPermissions
}
The next function uses the output of the previous to build the menu.
Function SelectMailboxPermission($mbox) {
$rights = (AuditSingleMailboxPermission -mbox $mbox).User
Write-Host $rights
$menu = @{}
for ($i=1;$i -le $rights.count; $i++) {
Write-Host "$i. $($rights[$i-1])"
$menu.Add($i,($rights[$i-1]))
}
[int]$ans = Read-Host "Enter selection"
$selection = $menu.Item($ans)
Return $selection
}
The Write-Host $rights line shows the full username. However, the menu only returns the first letter of the username. This is a problem because the output of this function is used to remove user permissions on a mailbox. I can't figure this out for the life of me.
powershell
I'm having trouble with creating a dynamic menu from mailbox permission entries in PowerShell. The below function gets a list of permission entries for the selected mailbox and outputs a table.
Function AuditSingleMailboxPermission($mbox) {
If ($mbox -like $null){
Write-Host "Please select the mailbox from the list below"
$mbox = SelectMailbox
}
$mboxPermissions = Get-MailboxPermission $mbox | where {$_.User -like "*@*"}
Return $mboxPermissions
}
The next function uses the output of the previous to build the menu.
Function SelectMailboxPermission($mbox) {
$rights = (AuditSingleMailboxPermission -mbox $mbox).User
Write-Host $rights
$menu = @{}
for ($i=1;$i -le $rights.count; $i++) {
Write-Host "$i. $($rights[$i-1])"
$menu.Add($i,($rights[$i-1]))
}
[int]$ans = Read-Host "Enter selection"
$selection = $menu.Item($ans)
Return $selection
}
The Write-Host $rights line shows the full username. However, the menu only returns the first letter of the username. This is a problem because the output of this function is used to remove user permissions on a mailbox. I can't figure this out for the life of me.
powershell
powershell
edited Nov 22 at 14:05
Robert
4,0371251106
4,0371251106
asked Nov 22 at 13:59
OhSnapWord
102
102
Try$menu.Add($i,$($rights[$i-1]))
. I think you forgot the dollar sign there
– Theo
Nov 22 at 14:11
add a comment |
Try$menu.Add($i,$($rights[$i-1]))
. I think you forgot the dollar sign there
– Theo
Nov 22 at 14:11
Try
$menu.Add($i,$($rights[$i-1]))
. I think you forgot the dollar sign there– Theo
Nov 22 at 14:11
Try
$menu.Add($i,$($rights[$i-1]))
. I think you forgot the dollar sign there– Theo
Nov 22 at 14:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
If (AuditSingleMailboxPermission -mbox $mbox).User
returns only one item, it will return a single object, not an array with length of one. Then, when you refer to $rights[0]
it won't return the first object of an array, but the first character of the String
representation of the object.
Casting to Array
should fix the issue:
$rights = [Array](AuditSingleMailboxPermission -mbox $mbox).User
Generic example:
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 1)[0]
F
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 2)[0]
FOO
1
+1 for the explanation and an effective solution, but the PowerShell-idiomatic solution would be to use@(AuditSingleMailboxPermission -mbox $mbox).User
, i.e., use of@(...)
, the array-subexpression operator, which guarantees that a command's output is treated as an array, if it isn't already one.
– mklement0
Nov 22 at 20:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If (AuditSingleMailboxPermission -mbox $mbox).User
returns only one item, it will return a single object, not an array with length of one. Then, when you refer to $rights[0]
it won't return the first object of an array, but the first character of the String
representation of the object.
Casting to Array
should fix the issue:
$rights = [Array](AuditSingleMailboxPermission -mbox $mbox).User
Generic example:
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 1)[0]
F
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 2)[0]
FOO
1
+1 for the explanation and an effective solution, but the PowerShell-idiomatic solution would be to use@(AuditSingleMailboxPermission -mbox $mbox).User
, i.e., use of@(...)
, the array-subexpression operator, which guarantees that a command's output is treated as an array, if it isn't already one.
– mklement0
Nov 22 at 20:20
add a comment |
up vote
1
down vote
accepted
If (AuditSingleMailboxPermission -mbox $mbox).User
returns only one item, it will return a single object, not an array with length of one. Then, when you refer to $rights[0]
it won't return the first object of an array, but the first character of the String
representation of the object.
Casting to Array
should fix the issue:
$rights = [Array](AuditSingleMailboxPermission -mbox $mbox).User
Generic example:
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 1)[0]
F
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 2)[0]
FOO
1
+1 for the explanation and an effective solution, but the PowerShell-idiomatic solution would be to use@(AuditSingleMailboxPermission -mbox $mbox).User
, i.e., use of@(...)
, the array-subexpression operator, which guarantees that a command's output is treated as an array, if it isn't already one.
– mklement0
Nov 22 at 20:20
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If (AuditSingleMailboxPermission -mbox $mbox).User
returns only one item, it will return a single object, not an array with length of one. Then, when you refer to $rights[0]
it won't return the first object of an array, but the first character of the String
representation of the object.
Casting to Array
should fix the issue:
$rights = [Array](AuditSingleMailboxPermission -mbox $mbox).User
Generic example:
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 1)[0]
F
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 2)[0]
FOO
If (AuditSingleMailboxPermission -mbox $mbox).User
returns only one item, it will return a single object, not an array with length of one. Then, when you refer to $rights[0]
it won't return the first object of an array, but the first character of the String
representation of the object.
Casting to Array
should fix the issue:
$rights = [Array](AuditSingleMailboxPermission -mbox $mbox).User
Generic example:
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 1)[0]
F
PS C:> ("FOO", "BAR", "BAZ" | Select-Object -First 2)[0]
FOO
edited Nov 22 at 14:36
answered Nov 22 at 14:31
Janne Tuukkanen
80329
80329
1
+1 for the explanation and an effective solution, but the PowerShell-idiomatic solution would be to use@(AuditSingleMailboxPermission -mbox $mbox).User
, i.e., use of@(...)
, the array-subexpression operator, which guarantees that a command's output is treated as an array, if it isn't already one.
– mklement0
Nov 22 at 20:20
add a comment |
1
+1 for the explanation and an effective solution, but the PowerShell-idiomatic solution would be to use@(AuditSingleMailboxPermission -mbox $mbox).User
, i.e., use of@(...)
, the array-subexpression operator, which guarantees that a command's output is treated as an array, if it isn't already one.
– mklement0
Nov 22 at 20:20
1
1
+1 for the explanation and an effective solution, but the PowerShell-idiomatic solution would be to use
@(AuditSingleMailboxPermission -mbox $mbox).User
, i.e., use of @(...)
, the array-subexpression operator, which guarantees that a command's output is treated as an array, if it isn't already one.– mklement0
Nov 22 at 20:20
+1 for the explanation and an effective solution, but the PowerShell-idiomatic solution would be to use
@(AuditSingleMailboxPermission -mbox $mbox).User
, i.e., use of @(...)
, the array-subexpression operator, which guarantees that a command's output is treated as an array, if it isn't already one.– mklement0
Nov 22 at 20:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432617%2fpowershell-dynamic-menu-based-on-mailbox-permission-entries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
$menu.Add($i,$($rights[$i-1]))
. I think you forgot the dollar sign there– Theo
Nov 22 at 14:11