ShiftNick

Disable-CompanyMailbox

Jul 24th, 2015
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Disable-CompanyMailbox
  2. <# 
  3. .DESCRIPTION
  4.     Hides email address from Global Address list and gives manager access to user mailbox if a Manager is assigned in AD. Converts to a shared mailbox
  5. .SYNOPSIS
  6.     Disables user mailbox
  7. .PARAMETER UserName
  8.     The name of the user to disable
  9. .EXAMPLE
  10.     Disable-CompanyMailbox -Username [Name]
  11. .NOTES
  12. Disable-CompanyMailboxV2.ps1 - Created by ShiftNick
  13. #>
  14. {
  15. [CmdletBinding()]
  16.     Param(
  17.        
  18.         [Parameter(Mandatory = $true,Position = 1,valueFromPipeline = $true)]
  19.         [String] $Username
  20.  
  21.     )
  22.  
  23.        
  24.         #Get UPN of user being disabled
  25.         $UPN = Get-ADUser -Identity $Username -Properties UserPrincipalName | select -ExpandProperty UserPrincipalName
  26.  
  27.         #Imports session to On-Prem Exchange server
  28.         $OnPremSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeServerName/PowerShell/ -Authentication Kerberos -ErrorAction:SilentlyContinue
  29.         Import-PSSession $OnPremSession
  30.  
  31.         #Checks for existence of Remote Mailbox On-Prem
  32.         $remoteMB = Get-RemoteMailbox -Identity $Username
  33.  
  34.             If ($remoteMB -eq $true)
  35.             {
  36.                 #Hide Address from ADdress Lists
  37.                 Set-RemoteMailbox -Identity $Username -HiddenFromAddressListsEnabled $true -ErrorAction:SilentlyContinue
  38.                
  39.                 Write-Output "The mailbox for, $Username, has been hidden from Exchange Lists"
  40.  
  41.                 #Removes remote session to exchange server
  42.                 Get-PSSession | Remove-PSSession -ErrorAction:SilentlyContinue
  43.                    
  44.                 #Open Connection to Office 365
  45.                 Import-Module MSOnline
  46.                 #Admin Credential for O365
  47.                 $O365Cred = Get-Credential -message "Enter your Office 365 Admin Credentials"
  48.                 $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection
  49.                 Import-PSSession $O365Session -AllowClobber
  50.                 Connect-MsolService –Credential $O365Cred        
  51.          
  52.                 #Check for existance for Mailbox
  53.                 $EmailAddress = Get-Mailbox -Identity $UPN -ErrorAction:SilentlyContinue
  54.            
  55.                 If ($EmailAddress -eq $null)
  56.                     {
  57.                         Write-Output "There is no mailbox for $UPN"
  58.                     }   #End Check O365 for Mailbox
  59.  
  60.                         else
  61.                             {
  62.                                 #Checks for assigned manager to user
  63.                                 $Manager = (Get-Aduser (Get-Aduser -identity $Username -Properties Manager | Select-Object Manager -ErrorAction:SilentlyContinue).Manager).samAccountName | Out-Null
  64.  
  65.                                 #Get license assigned to user
  66.                                 $license = Get-MsolUser -UserPrincipalName $UPN | select -ExpandProperty licenses
  67.                
  68.                                 IF ($Manager -eq $Null)
  69.                                 {
  70.                                     #Remove License from account
  71.                                     Set-MsolUserLicense -UserPrincipalName $UPN -RemoveLicenses $license.AccountSkuId
  72.  
  73.                                     #Convert to shared mailbox
  74.                                     Set-Mailbox -Identity $UPN -Type shared
  75.  
  76.                                     #Inform that no Manager is assigned
  77.                                     Write-Output "$username does not have a manager assigned"
  78.  
  79.                                     #Removes remote session to exchange server
  80.                                     Get-PSSession | Remove-PSSession -ErrorAction:SilentlyContinue
  81.                                 }   #End Check for Manager
  82.                
  83.                                 else
  84.                                     {
  85.                                         #Grants manager access to user mailbox
  86.                                         Add-MailboxPermission -Identity $Username -User $Manager -AccessRights Fullaccess -InheritanceType all
  87.  
  88.                                         #Remove License from account
  89.                                         Set-MsolUserLicense -UserPrincipalName $UPN -RemoveLicenses $license.AccountSkuId
  90.  
  91.                                         #Convert to shared mailbox
  92.                                         Set-Mailbox -Identity $UPN -Type shared
  93.                    
  94.                                         Write-Output "$Manager has full access to the Mailbox of $Username"
  95.                    
  96.                                         #Removes remote session to exchange server
  97.                                         Get-PSSession | Remove-PSSession -WarningAction:SilentlyContinue
  98.                    
  99.                                     }   #End grant Manager access            
  100.                
  101.                                 }   #End check for Manager
  102.  
  103.             } # End Checks for existence of Mailboxes
  104.                
  105.             else
  106.                 {
  107.                     #Removes remote session to exchange server
  108.                     Get-PSSession | Remove-PSSession -ErrorAction:SilentlyContinue
  109.                 }
  110.            
  111. }   #End Disable-CompanyMailbox Function
Advertisement
Add Comment
Please, Sign In to add comment