0

I was testing the control flow of my script to see what direction I was going with it, and got some strange results. At first it all seemed to be working correctly, but I decided to test the other if statement first, and after this run I ran the script again and followed the other IF statement which now will not work. I did not change anything in the script. I think there must be something up with one of my statements. The first conditional statement uses a nested IF statement and the second does not. Both ways work on the first run. A link is below to see the output.Script runs

$Optcmd = Read-Host -Prompt "Does the User/s require permissions set to the whole [M]ailbox or to a specific [F]older within the mailbox?[M/F]"

if ($Optcmd -like "m"){
    $confmailbox = Read-Host -Prompt "The permission you set here will give the User/s the same level of access to the other folders and calendar within the mailbox. Continue?[Y/N]"
    }
    else {
$confmfoldper = Read-Host -Prompt "You have selected to apply permissions to a specific folder continue? [Y/N]?"
}
if ($confmailbox -like "y"){
    Write-Host -ForegroundColor Yellow "What access rights does the user/s require? "
    Write-Host -ForegroundColor Green "[1] ChangeOwner - "-NoNewline
    Write-Host -ForegroundColor Yellow " The user who can change the mailbox owner permission"
    Write-Host -ForegroundColor Green "[2] DeleteItem - " -NoNewline
    Write-Host -ForegroundColor Yellow " The user who can only delete items in the mailbox"
    Write-Host -ForegroundColor Green "[3] ExternalAccount - " -NoNewline    
    Write-Host -ForegroundColor Yellow " The user is not in the same organization"                                                
    Write-Host -ForegroundColor Green "[4] FullAccess - " -NoNewline
    Write-Host -ForegroundColor Yellow " The user who can do everything in this mailbox"                                            
    Write-Host -ForegroundColor Green "[5] ReadPermission - " -NoNewline                                                 
    Write-Host -ForegroundColor Yellow " The user who can only read everything in the mailbox"
   
    $Boxperm = Read-Host  -prompt "Enter corresponding number" 
        if ($Boxperm -eq 1){$Boxperm = 'ChangeOwner'}
        if ($Boxperm -eq 2){$Boxperm = 'DeleteItem'}
        if ($Boxperm -eq 3){$Boxperm = 'ExternalAccount'}
        if ($Boxperm -eq 4){$Boxperm = 'FullAccess'}
        if ($Boxperm -eq 5){$Boxperm = 'ReadPermission'
        }
        $Boxperm
}
Elseif ($confmfoldper -like 'y')  {

    Write-Host -ForegroundColor Yellow "Select a role to assign to the User/s "
    Write-Host -ForegroundColor Green "[1] Author - "-NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, DeleteOwnedItems, EditOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[2] Contributor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, FolderVisible"
    Write-Host -ForegroundColor Green "[3] Editor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, DeleteAllItems, DeleteOwnedItems, EditAllItems, EditOwnedItems, FolderVisible, ReadItems" 
    Write-Host -ForegroundColor Green "[4] NonEditingAuthor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, DeleteOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[5] Owner - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, CreateSubfolders, DeleteAllItems, DeleteOwnedItems, EditAllItems, EditOwnedItems, FolderContact, FolderOwner, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[6] PublishingAuthor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, CreateSubfolders, DeleteOwnedItems, EditOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[7] PublishingEditor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, CreateSubfolders, DeleteAllItems, DeleteOwnedItems, EditAllItems, EditOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[8] Reviewer - " -NoNewline
    Write-Host -ForegroundColor Yellow "FolderVisible, ReadItems"
    
    $Folderperm = Read-Host  -prompt "Enter corresponding number"
       
    $Folderperm 
    }

Updated Script:

param(

[String]$Useremail,
[String]$ImportPath
)

$confmailbox = $null
$Folderperm = $null
$confmfoldper = $null
$Folder = $null

 
$Mailbox = Read-Host “Enter Mailbox ID “
$Displayname = Get-Mailbox -Identity $Mailbox
$Optcmd = Read-Host -Prompt "Does the User/s require permissions set to the whole [M]ailbox or to a specific [F]older within the mailbox?[M/F]"

if ($ImportPath -ne "") {
    $ImportCSV = Import-Csv $ImportPath
    }

if ($Optcmd -like "m"){
    $confmailbox = Read-Host -Prompt "The permission you set here will give the User/s the same level of access to the other folders and calendar within the mailbox. Continue?[Y/N]"
    }
elseif ($Optcmd -like "f") {
    $confmfoldper = Read-Host -Prompt "You have selected to apply permissions to a specific folder continue? [Y/N]?"
}
if ($confmailbox -like "y"){
    Write-Host -ForegroundColor Yellow "What access rights does the user/s require? "
    Write-Host -ForegroundColor Green "[1] ChangeOwner - "-NoNewline
    Write-Host -ForegroundColor Yellow " The user who can change the mailbox owner permission"
    Write-Host -ForegroundColor Green "[2] DeleteItem - " -NoNewline
    Write-Host -ForegroundColor Yellow " The user who can only delete items in the mailbox"
    Write-Host -ForegroundColor Green "[3] ExternalAccount - " -NoNewline    
    Write-Host -ForegroundColor Yellow " The user is not in the same organization"                                                
    Write-Host -ForegroundColor Green "[4] FullAccess - " -NoNewline
    Write-Host -ForegroundColor Yellow " The user who can do everything in this mailbox"                                            
    Write-Host -ForegroundColor Green "[5] ReadPermission - " -NoNewline                                                 
    Write-Host -ForegroundColor Yellow " The user who can only read everything in the mailbox"
    
    $Boxperm = Read-Host  -prompt "Enter corresponding number"
    
    }
   
     switch($Boxperm)
     {
     {$_ -eq 1} {$Boxperm = 'ChangeOwner' ; break;}
     {$_ -eq 2} {$Boxperm = 'DeleteItem' ; break;}
     {$_ -eq 3} {$Boxperm = 'ExternalAccount' ; break;}
     {$_ -eq 4} {$Boxperm = 'FullAccess' ; break;}
     {$_ -eq 5} {$Boxperm = 'ReadPermission' ; break;}
     }
     
if ($confmfoldper -like 'y')  {

    Write-Host -ForegroundColor Yellow "Select a role to assign to the User/s "
    Write-Host -ForegroundColor Green "[1] Author - "-NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, DeleteOwnedItems, EditOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[2] Contributor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, FolderVisible"
    Write-Host -ForegroundColor Green "[3] Editor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, DeleteAllItems, DeleteOwnedItems, EditAllItems, EditOwnedItems, FolderVisible, ReadItems" 
    Write-Host -ForegroundColor Green "[4] NonEditingAuthor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, DeleteOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[5] Owner - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, CreateSubfolders, DeleteAllItems, DeleteOwnedItems, EditAllItems, EditOwnedItems, FolderContact, FolderOwner, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[6] PublishingAuthor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, CreateSubfolders, DeleteOwnedItems, EditOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[7] PublishingEditor - " -NoNewline
    Write-Host -ForegroundColor Yellow "CreateItems, CreateSubfolders, DeleteAllItems, DeleteOwnedItems, EditAllItems, EditOwnedItems, FolderVisible, ReadItems"
    Write-Host -ForegroundColor Green "[8] Reviewer - " -NoNewline
    Write-Host -ForegroundColor Yellow "FolderVisible, ReadItems"
    
    $Folderperm = Read-Host  -prompt "Enter corresponding number"
    $Folder = Read-Host “Enter the FOLDER NAME ( Examplles : Inbox,calendar…)”
    $foldername = $Displayname.WindowsEmailAddress + “:\” + $Folder
    
    }

    switch($Folderperm)
    {
    {$Folderperm -eq 1} {$Folderperm = 'Author' ; break; }
    {$Folderperm -eq 2} {$Folderperm = 'Contributor' ; break;}
    {$Folderperm -eq 3} {$Folderperm = 'Editor' ; break;}
    {$Folderperm -eq 4} {$Folderperm = 'NonEditingAuthor' ; break;}
    {$Folderperm -eq 5} {$Folderperm = 'Owner' ; break;}
    {$Folderperm -eq 6} {$Folderperm = 'PublishingAuthor' ; break;}
    {$Folderperm -eq 7} {$Folderperm = 'PublishingEditor' ; break;}
    {$Folderperm -eq 8} {$Folderperm = 'Reviewer' ; break;}


    }



    if ($confmailbox -like 'y' -and $ImportPath -eq "") {
    Add-MailboxPermission -identity $Displayname.Name -user $Useremail -AccessRights $Boxperm -InheritanceType All -Automapping $false
    }

    If ($Folder -ne “” -and $ImportPath -ne "") {
    Add-MailboxFolderPermission $foldername -User $Useremail -AccessRights $Foldperm -confirm:$true
    }

     if ($confmailbox -like 'y' -and $ImportPath -ne "") {
    
     foreach ($User in $ImportCSV){
     Add-MailboxPermission -identity $Displayname.Name -user $User.Email -AccessRights $Boxperm -InheritanceType All -Automapping $false
    }
     }


If ($Folder -ne “” -and $ImportPath -ne "") { 

foreach ($User in $ImportCSV) {
    Add-MailboxFolderPermission $foldername -User $User -AccessRights $Foldperm -confirm:$true
        }
    }

I have had a chance to test out the ImportCSV parameter with the nested ForEach statements yet. However, the script works using -Useremail parameter.

  • You need to explain what the expected behaviour is and why what is currently happening is wrong. – Tom Oct 17 '21 at 20:01
  • When I run the script the first time I set $Optcmd = “F” triggering $confmfoldper which is also set to “Y” then the IF statement is correctly skipped and the Elseif statement is correctly triggered setting the variable $Folderperm to the corresponding index. I run the script again this time I set $Optcmd = “M” triggering the IF statement and correctly setting the variable $Boxperm to the corresponding user selected index. It goes wrong when I again run the script and set $Optcmd to “F” again. Instead of triggering the elseif, it triggers the IF statement. See the screenshot I attached. – Get-Drink_-Energy_Redbull Oct 18 '21 at 22:44
  • When I run your example it works correctly. In your screenshot there is additional text (where the user name is listed) before the question "Does the User/s require permissions set to the whole [M]ailbox or to a specific [F]older within the mailbox?[M/F]" This tells me the code you've included is not the same as was run for the screenshot. Take your example code above and prove it behaves the same way as the screenshot. – Tom Oct 19 '21 at 16:41
  • The problem was simple. You are correct, the script was incomplete. I would be adding a loop to this script to restart the whole thing. The problem is that once the variables had been set on the first run they would still trigger my other If statements because the variable is set from the previous run. I resolved this by adding $variable = $null for the problematic variables at the beginning of my script. – Get-Drink_-Energy_Redbull Oct 20 '21 at 19:57
  • I also switched out the $Boxperm ElseIf statements with: switch($Boxperm) { {$_ -eq 1} {$Boxperm = 'ChangeOwner' ; break;} {$_ -eq 2} {$Boxperm = 'DeleteItem' ; break;} {$_ -eq 3} {$Boxperm = 'ExternalAccount' ; break;} {$_ -eq 4} {$Boxperm = 'FullAccess' ; break;} {$_ -eq 5} {$Boxperm = 'ReadPermission' ; break;} – Get-Drink_-Energy_Redbull Oct 20 '21 at 20:05
  • Glad to hear it's fixed. – Tom Oct 20 '21 at 21:05

0 Answers0