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.










share|improve this question
























  • Try $menu.Add($i,$($rights[$i-1])). I think you forgot the dollar sign there
    – Theo
    Nov 22 at 14:11















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.










share|improve this question
























  • Try $menu.Add($i,$($rights[$i-1])). I think you forgot the dollar sign there
    – Theo
    Nov 22 at 14:11













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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





share|improve this answer



















  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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





share|improve this answer



















  • 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

















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





share|improve this answer



















  • 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















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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo