locked
Adding Checks to script for mailbox RRS feed

  • Question

  • Hi All,

    I am new to scripting and exchange so excuse my lack of knowledge.

    I have found exactly what I am after for applying Reviewer permission to an entire mailbox from: http://exchangeserverpro.com/grant-read-access-exchange-mailbox/ - Thanks to Paul Cunningham.

    However I need to be able to add some checks into the script and also custom comments?

    For example, 

    After each of the below I want to add a check, to see if the mailbox exists? if so just go to Parameter 2 if not give me an error of "Mailbox does not exist or spelling is incorrect"

    [Parameter( Mandatory=$true)]
    [string]$Mailbox

    After the below I would like to check to see if the group is mail-enabled, if so just to apply the permissions as per the below script, if it is not mail enabled to give me an error of" Group is not mail-enabled"

    [Parameter( Mandatory=$true)]
    [string]$REV Group

    Again SORRY for my lack of knowledge, however I am hoping to learn and get a foot in on where to start.

    Thanks in advance.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Script:

    #Proof of concept code to apply mailbox
    #http://exchangeserverpro.com/grant-read-access-exchange-mailbox/
    #Applies "Reviewer" permissions to all folders in a mailbox

    [CmdletBinding()]
    param (
    [Parameter( Mandatory=$true)]
    [string]$Mailbox
        
    [Parameter( Mandatory=$true)]
    [string]$REV Group
        
      #[Parameter( Mandatory=$true)]
    #[string]$Access
    )

    $exclusions = @("/Sync Issues",
                    "/Sync Issues/Conflicts",
                    "/Sync Issues/Local Failures",
                    "/Sync Issues/Server Failures",
                    "/Recoverable Items",
                    "/Deletions",
                    "/Purges",
                    "/Versions"
                    )



    $mailboxfolders = @(Get-MailboxFolderStatistics $Mailbox | Where {!($exclusions -icontains 

    $_.FolderPath)} | Select FolderPath)

    foreach ($mailboxfolder in $mailboxfolders)
    {
        $folder = $mailboxfolder.FolderPath.Replace("/","\")
        if ($folder -match "Top of Information Store")
        {
           $folder = $folder.Replace(“\Top of Information Store”,”\”)
        }
        $identity = "$($mailbox):$folder"
        Write-Host "Adding $user to $identity with Reviewer permissions"
        Add-MailboxFolderPermission -Identity $identity -User $REV Group -AccessRights Reviewer -

    ErrorAction SilentlyContinue
    }

    Sunday, June 19, 2016 11:40 PM

Answers

  • After each of the below I want to add a check, to see if the mailbox exists? if so just go to Parameter 2 if not give me an error of "Mailbox does not exist or spelling is incorrect

    I don't really understand why you want to go to Parameter 2 when Parameter 1 is invalid. However, you can add some if statements to the script to archive what you want. It's not the best coding solution, but it should work for you.

    [CmdletBinding()]
    param (
        [Parameter( Mandatory = $true)]
        [string]$Mailbox,
        
        [Parameter( Mandatory = $true)]
        [string]$REVGroup
        
        #[Parameter( Mandatory=$true)]
        #[string]$Access
    )
    
    $exclusions = @('/Sync Issues', 
        '/Sync Issues/Conflicts', 
        '/Sync Issues/Local Failures', 
        '/Sync Issues/Server Failures', 
        '/Recoverable Items', 
        '/Deletions', 
        '/Purges', 
        '/Versions'
    )
    
    
    if ((get-mailbox $Mailbox) -eq $null)
    {
        Write-Host -Object '[ERROR] Mailbox not found.' -ForegroundColor Red
    }
    else 
    {
        if ((Get-DistributionGroup $REVGroup) -eq $null)
        {
            Write-Host -Object '[ERROR] Group is not mail-enabled.' -ForegroundColor Red
        }
        else
        {
            $mailboxfolders = @(Get-MailboxFolderStatistics $Mailbox |
                Where-Object -FilterScript {
                    !($exclusions -icontains 
    
                    $_.FolderPath)
                } |
            Select-Object -Property FolderPath)
    
            foreach ($mailboxfolder in $mailboxfolders)
            {
                $folder = $mailboxfolder.FolderPath.Replace('/','\')
                if ($folder -match 'Top of Information Store')
                {
                    $folder = $folder.Replace('\Top of Information Store','\')
                }
                $identity = "$($Mailbox):$folder"
                Write-Host -Object "Adding $user to $identity with Reviewer permissions"
                Add-MailboxFolderPermission -Identity $identity -User $REVGroup -AccessRights Reviewer -
    
                ErrorAction SilentlyContinue
            }
        }
    }
    

    • Proposed as answer by Vincent Karunaidas Monday, June 20, 2016 1:53 PM
    • Marked as answer by Lynn-Li Friday, July 8, 2016 2:29 AM
    Monday, June 20, 2016 8:14 AM

All replies

  • After each of the below I want to add a check, to see if the mailbox exists? if so just go to Parameter 2 if not give me an error of "Mailbox does not exist or spelling is incorrect

    I don't really understand why you want to go to Parameter 2 when Parameter 1 is invalid. However, you can add some if statements to the script to archive what you want. It's not the best coding solution, but it should work for you.

    [CmdletBinding()]
    param (
        [Parameter( Mandatory = $true)]
        [string]$Mailbox,
        
        [Parameter( Mandatory = $true)]
        [string]$REVGroup
        
        #[Parameter( Mandatory=$true)]
        #[string]$Access
    )
    
    $exclusions = @('/Sync Issues', 
        '/Sync Issues/Conflicts', 
        '/Sync Issues/Local Failures', 
        '/Sync Issues/Server Failures', 
        '/Recoverable Items', 
        '/Deletions', 
        '/Purges', 
        '/Versions'
    )
    
    
    if ((get-mailbox $Mailbox) -eq $null)
    {
        Write-Host -Object '[ERROR] Mailbox not found.' -ForegroundColor Red
    }
    else 
    {
        if ((Get-DistributionGroup $REVGroup) -eq $null)
        {
            Write-Host -Object '[ERROR] Group is not mail-enabled.' -ForegroundColor Red
        }
        else
        {
            $mailboxfolders = @(Get-MailboxFolderStatistics $Mailbox |
                Where-Object -FilterScript {
                    !($exclusions -icontains 
    
                    $_.FolderPath)
                } |
            Select-Object -Property FolderPath)
    
            foreach ($mailboxfolder in $mailboxfolders)
            {
                $folder = $mailboxfolder.FolderPath.Replace('/','\')
                if ($folder -match 'Top of Information Store')
                {
                    $folder = $folder.Replace('\Top of Information Store','\')
                }
                $identity = "$($Mailbox):$folder"
                Write-Host -Object "Adding $user to $identity with Reviewer permissions"
                Add-MailboxFolderPermission -Identity $identity -User $REVGroup -AccessRights Reviewer -
    
                ErrorAction SilentlyContinue
            }
        }
    }
    

    • Proposed as answer by Vincent Karunaidas Monday, June 20, 2016 1:53 PM
    • Marked as answer by Lynn-Li Friday, July 8, 2016 2:29 AM
    Monday, June 20, 2016 8:14 AM
  • Thanks Vincent.

    It's not so much as to move to Parameter 2 when Parameter 1 is invalid, it would be so that when Parameter 1 is invalid, it says "Mailbox not found" and goes back to Parameter 1. Same deal again for Parameter 2.

    if that makes sense hahaha

    This has helped alot but, and was exactly the stepping platform needed.

    Thank you very much for your assistance!!!!

    Monday, June 20, 2016 9:46 PM
  • Hi,

    Please remember to mark helpful reply as answer to close this thread.

    Thanks for cooperation.

    Best Regards.


    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    Lynn-Li
    TechNet Community Support

    Tuesday, June 21, 2016 2:08 AM