none
Optimizing several get-mailboxfolderpermission in script RRS feed

  • Question

  • Hello,

    I have a script that contains several get-mailboxfolderpermission lines for different folder as show here

    get-mailboxfolderpermission "$user`:\calendar"
    
    get-mailboxfolderpermission "$user`:\calendrier"
    
    get-mailboxfolderpermission "$user`:\inbox"
    
    get-mailboxfolderpermission "$user`:\boîte de réception"

    I'd like to optimize those line to avoid repeating the command over and have a time saving overall.

    So I'm looking to have Something like this

    $permission = get-mailboxfolderpermission $user
    
    $permission :\calendar
    
    $permission :\callendrier
    
    $permission :\inbox
    
    etc...

    Of course, I'm quite aware that the way shown doesn't work, I've tried different thing to no avail unfortunatly.

    I'd like to know if it's possible to do it that way first of all, and if so, have some hing to make it work please.

    Thank you

    Nad

    Thursday, August 22, 2019 4:05 PM

All replies

  • Start by learning the very most basic things in PowerShell. There are many tutorials and books. The question you are asking is a pre-school level question that you should know before trying to write a script.

    help foreach-object -online


    \_(ツ)_/

    Thursday, August 22, 2019 5:15 PM
    Moderator
  • Hi,

    Thanks for your question.

    You can save the folder path or the <Mailbox>:\<Folder> value in the array, and then use foreach-object cmdlet to execute it.

    For example:

    $array1="\calendar","\callendrier","\inbox"

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-6

    https://powertoe.wordpress.com/2009/12/14/powershell-part-4-arrays-and-for-loops/

    Best regards,

    Lee


    Just do it.

    Friday, August 23, 2019 3:00 AM
    Moderator
  • Thank for your answer LeeSeenLi, 

    Although I haven't thought about array and loops, I'm not certain it would work.

    I think it would've been better if I'd shown the original script to better understand my request.

    if (get-mailboxfolderpermission "$user`:\calendar" -ErrorAction ignore)
        {
            $mailcal = (get-mailboxfolderpermission "$user`:\calendar" | ft -AutoSize | out-string).trim()
        }
        Else
        {
           $mailcal = (get-mailboxfolderpermission "$user`:\calendrier" | ft -AutoSize | out-string).trim()
        }
        
        $Contacts = "##################################### Contacts ##################################### "
        $MailBoxPerm1 = (get-mailboxfolderpermission "$user`:\contacts" -ErrorAction ignore | ft -AutoSize | out-string).trim()
    
        $inbox = "##################################### Inbox ##################################### "
        if (get-mailboxfolderpermission "$user`:\inbox" -ErrorAction ignore)
        {
            $MailFoldPerm2 = (get-mailboxfolderpermission "$user`:\inbox" | ft -AutoSize | out-string).trim()
        }
        Else
        {
            $MailFoldPerm2 = (get-mailboxfolderpermission "$user`:\boîte de réception" | ft -AutoSize | out-string).trim()
        }

    So this is what I'm trying to fully optimize. do you think Try/Catch method would be better or you have another suggestion maybe?

    Thanks

    Monday, August 26, 2019 8:31 AM
  • Hi,

    Thanks for your response.

    About if/else statement, please refer the link below:

    https://ss64.com/ps/if.html

    Syntax
          if ( condition ) { commands_to_execute }
          [ elseif ( condition2 ) { commands_to_execute } ]
          [ else {commands_to_execute} ]

    Your if else statement do nothing. The commands_to_execute in your if and else are the same.

    About your original question, if you want to optimize the Duplicate commands, you can use foreach loop.

    About try/catch statement, it used to respond to or handle terminating errors in scripts. 

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6

    "\calendar","\callendrier","\inbox" | ForEach-Object {(get-mailboxfolderpermission "$user`:$_" | ft -AutoSize | out-string).trim()}

    Best regards,

    Lee


    Just do it.

    Tuesday, August 27, 2019 7:12 AM
    Moderator
  • Hi

    Thank you for your answer LeeSeenLi. thankfully with the hint given, I came up with that final solution which work nicely.

    $Folders = "\calendar","\Calendrier","\Contacts","\inbox","\boîte de réception"
        $FolderPermission = $Folders | ForEach-Object {(get-mailboxfolderpermission "$user`:$_" -ErrorAction ignore | ft -AutoSize | out-string).trim()}
    
        $Calendar = "##################################### Calendar #####################################"
        if ($mailboxpermission -match "Calendrier")
        {
            $mailcal = $mailboxpermission -match "Calendrier"
        }
        Else
        {
           $mailcal = $mailboxpermission -match "Calendar"
        }
        
        $Contacts = "##################################### Contacts ##################################### "
        $MailBoxPerm1 = $mailboxpermission -match "Contacts"
    
        $inbox = "##################################### Inbox ##################################### "
        if ($mailboxpermission -match "inbox")
        {
            $MailFoldPerm2 = $mailboxpermission -match "Inbox"
        }
        Else
        {
            $MailFoldPerm2 = $mailboxpermission -match "boîte de réception"
        }

    @JRV I'm aware of what you said, thanks for pointing it out still.

    Thank you

    Nad

    Monday, September 2, 2019 11:46 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, September 4, 2019 8:00 AM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee



    Just do it.

    Friday, September 6, 2019 11:57 AM
    Moderator