Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Disable-CompanyMailbox
- <#
- .DESCRIPTION
- 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
- .SYNOPSIS
- Disables user mailbox
- .PARAMETER UserName
- The name of the user to disable
- .EXAMPLE
- Disable-CompanyMailbox -Username [Name]
- .NOTES
- Disable-CompanyMailboxV2.ps1 - Created by ShiftNick
- #>
- {
- [CmdletBinding()]
- Param(
- [Parameter(Mandatory = $true,Position = 1,valueFromPipeline = $true)]
- [String] $Username
- )
- #Get UPN of user being disabled
- $UPN = Get-ADUser -Identity $Username -Properties UserPrincipalName | select -ExpandProperty UserPrincipalName
- #Imports session to On-Prem Exchange server
- $OnPremSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeServerName/PowerShell/ -Authentication Kerberos -ErrorAction:SilentlyContinue
- Import-PSSession $OnPremSession
- #Checks for existence of Remote Mailbox On-Prem
- $remoteMB = Get-RemoteMailbox -Identity $Username
- If ($remoteMB -eq $true)
- {
- #Hide Address from ADdress Lists
- Set-RemoteMailbox -Identity $Username -HiddenFromAddressListsEnabled $true -ErrorAction:SilentlyContinue
- Write-Output "The mailbox for, $Username, has been hidden from Exchange Lists"
- #Removes remote session to exchange server
- Get-PSSession | Remove-PSSession -ErrorAction:SilentlyContinue
- #Open Connection to Office 365
- Import-Module MSOnline
- #Admin Credential for O365
- $O365Cred = Get-Credential -message "Enter your Office 365 Admin Credentials"
- $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection
- Import-PSSession $O365Session -AllowClobber
- Connect-MsolService –Credential $O365Cred
- #Check for existance for Mailbox
- $EmailAddress = Get-Mailbox -Identity $UPN -ErrorAction:SilentlyContinue
- If ($EmailAddress -eq $null)
- {
- Write-Output "There is no mailbox for $UPN"
- } #End Check O365 for Mailbox
- else
- {
- #Checks for assigned manager to user
- $Manager = (Get-Aduser (Get-Aduser -identity $Username -Properties Manager | Select-Object Manager -ErrorAction:SilentlyContinue).Manager).samAccountName | Out-Null
- #Get license assigned to user
- $license = Get-MsolUser -UserPrincipalName $UPN | select -ExpandProperty licenses
- IF ($Manager -eq $Null)
- {
- #Remove License from account
- Set-MsolUserLicense -UserPrincipalName $UPN -RemoveLicenses $license.AccountSkuId
- #Convert to shared mailbox
- Set-Mailbox -Identity $UPN -Type shared
- #Inform that no Manager is assigned
- Write-Output "$username does not have a manager assigned"
- #Removes remote session to exchange server
- Get-PSSession | Remove-PSSession -ErrorAction:SilentlyContinue
- } #End Check for Manager
- else
- {
- #Grants manager access to user mailbox
- Add-MailboxPermission -Identity $Username -User $Manager -AccessRights Fullaccess -InheritanceType all
- #Remove License from account
- Set-MsolUserLicense -UserPrincipalName $UPN -RemoveLicenses $license.AccountSkuId
- #Convert to shared mailbox
- Set-Mailbox -Identity $UPN -Type shared
- Write-Output "$Manager has full access to the Mailbox of $Username"
- #Removes remote session to exchange server
- Get-PSSession | Remove-PSSession -WarningAction:SilentlyContinue
- } #End grant Manager access
- } #End check for Manager
- } # End Checks for existence of Mailboxes
- else
- {
- #Removes remote session to exchange server
- Get-PSSession | Remove-PSSession -ErrorAction:SilentlyContinue
- }
- } #End Disable-CompanyMailbox Function
Advertisement
Add Comment
Please, Sign In to add comment