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.