Advertisement
ScottB2211

Untitled

Aug 16th, 2024
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Script will change a user mailbox into a shared mailbox and prompt you if you want to add a delegate.
  2. # define functions
  3. function Get-UserInfo{
  4.    [string]$script:mailbox = Read-Host "please enter the email address of the user you should like to convert"
  5. }
  6.  
  7. function Set-SharedMbox {
  8.     Write-Host "Checking if $mailbox is already shared type..."
  9.     $state = $(Get-Mailbox -Identity $mailbox | Select-Object -ExpandProperty RecipientTypeDetails)
  10.  
  11.     if($state -eq "UserMailbox"){
  12.         Set-Mailbox -identity $mailbox -Type Shared
  13.         Write-Host "Successfully converted $mailbox to a shared mailbox."
  14.     }
  15.     elseif($state -eq "SharedMailbox"){
  16.         Write-Host "Mailbox $mailbox is already a shared mailbox." 
  17.     }
  18. }
  19.  
  20. function Edit-Delegates {
  21.     param (
  22.         [string[]]$delegate,
  23.         [string[]]$rights
  24.     )
  25.  
  26.     Add-MailboxPermission -Identity "$mailbox" -User "$delegate" -AccessRights FullAccess
  27.     if ($?){
  28.         Write-Host "Added user $delegate as a delegate of $mailbox."
  29.     }
  30.  
  31.     if ($rights -eq 2){
  32.         Add-RecipientPermission -Identity "$mailbox" -Trustee "$delegate" -AccessRights SendAs
  33.  
  34.         if ($?){
  35.             Write-Host "Added sending rights for $delegate on mailbox $mailbox."
  36.         }
  37.     }
  38. }
  39.  
  40. function Read-Choice {
  41.    $script:choice = Read-Host "Would you like to add a delegate user now?"
  42. }
  43.  
  44. # Actual script
  45.  
  46. $adminuser = Read-Host 'Enter the email address of an admin user'
  47.  
  48. Connect-ExchangeOnline -UserPrincipalName $adminuser
  49.  
  50. # clear the screen to avoid the cmdlet vomit
  51. Clear-Host
  52.  
  53. Get-UserInfo
  54.  
  55. if([string]::IsNullOrWhiteSpace($mailbox)){
  56.     Write-Host "You must enter an email address."
  57.     Write-Host "Please try again."
  58.     Get-UserInfo
  59.     }
  60. else{
  61.     Set-SharedMbox
  62. }
  63.  
  64. Read-Choice
  65.  
  66. if ($choice -eq "y"){
  67.     $delegate = Read-Host "Please enter the email address for the delegated user"
  68.     Write-Host "`n`n"
  69.     Write-Host "Please enter the access rights you would like this user to have."
  70.     Write-Host "1. FullAccess (full access to the mailbox, but cannot send.)"
  71.     Write-Host "2. FullAccess + SendAs (Same as above, but allows sending from the delegated mailbox.)"
  72.     $rights = Read-Host "Enter your choice"
  73.  
  74.     Clear-Host
  75.  
  76.     Edit-Delegates $delegate $rights
  77.  
  78. }
  79. elseif ($choice -eq "n"){
  80.     Write-Output "Goodbye."
  81. }
  82. else{
  83.     Write-Output "Invalid entry. Please try again."
  84.     Read-Choice
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement