Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $Script:userExists
- $Script:azureADConnected = $false
- $Script:O365Disabled = $false
- $Script:ADAccountEnabled = $true
- $Script:HiddenFromAddressList = $false
- $Script:ConnectedToO365 = $false
- $Script:EmailSentToVendor = $false
- $Script:ConvertedToSharedMailbox = $false
- function Prerequisites {
- "VERIFYING O365 MODULES"
- if(-not(Get-Module -ListAvailable -Name "MSONLINE"))
- {
- Write-Warning "MSONLINE MODULE DOES NOT EXIST"
- "ATTEMPTING TO INSTALL MODULE"
- try{
- Install-Module -Name MSONLINE
- }
- catch{
- "Failed to install module MSONLINE"
- Exit
- }
- }
- if(-not(Get-Module -ListAvailable -Name "ExchangeOnlineManagement"))
- {
- Write-Warning "ExchangeOnlineManagement MODULE DOES NOT EXIST"
- "ATTEMPTING TO INSTALL MODULE"
- try{
- Install-Module -Name ExchangeOnlineManagement
- }
- catch{
- "Failed to install module ExchangeOnlineManagement"
- Exit
- }
- }
- if(-not(Get-Module -ListAvailable -Name "AzureAD"))
- {
- Write-Warning "AzureAD MODULE DOES NOT EXIST"
- "ATTEMPTING TO INSTALL MODULE"
- try{
- Install-Module -Name AzureAD
- }
- catch{
- "Failed to install module AzureAD"
- Exit
- }
- }
- "All MODULES PRESENT"
- }
- function Send-Email {
- param (
- [string]
- $User,
- $ScriptRunnerEmail,
- $ScriptRunnerPassword,
- $To
- )
- $cred = New-Object System.Management.Automation.PSCredential -argumentlist $ScriptRunnerEmail, $ScriptRunnerPassword
- $body = ""
- try {
- Send-MailMessage -From $ScriptRunnerEmail -To $To -Cc "" -Subject "" -Body $body -SmtpServer "smtp.office365.com" -Port "587" -UseSsl -Credential $cred
- $Script:EmailSentToVendor = $true
- }
- catch {
- "Unable to send an email to vendor, send one manually to have the account disabled"
- }
- }
- function Disable-SignIn {
- param (
- [string]
- $User
- )
- #email of user to disable
- $upn = Get-ADUser -Identity $User -Properties * | Select-Object -ExpandProperty "UserPrincipalName"
- Set-AzureADUser -ObjectId $upn -AccountEnabled $false
- $isEnabled = Get-AzureADUser -ObjectId $upn | Select-Object -ExpandProperty AccountEnabled
- if ($isEnabled -eq $true) {
- "Unable to disable AD account: $upn"
- "Script has terminated"
- exit
- }
- #Check if sign in is blocked (O365)
- $Script:O365Disabled = (Get-MsolUser -UserPrincipalName $upn).blockcredential
- #Hide from GAL
- Set-ADUser -Identity $User -Replace @{msExchHideFromAddressLists = "TRUE" }
- #Verify attribute above was applied
- $Script:HiddenFromAddressList = Get-ADUser -Identity $User -Properties * | Select-Object -ExpandProperty msExchHideFromAddressLists
- }
- function Get-UserToOffboard {
- param (
- [string]
- $User
- )
- #Check if account exists in Active Directory
- if (@(Get-ADUser -Filter { SamAccountName -ne $User }).Count -eq 0) {
- $Script:userExists = $true
- }
- }
- function Invoke-DisableADAccount {
- param (
- [string]
- $User
- )
- Set-ADUser -Identity $User -Enabled $false
- }
- #Check if all Exchange Modules are available and will attempt to install, will terminate script if any of the modules aren't present
- Prerequisites
- $scriptUser = $env:USERNAME
- "Welcome $scriptUser"
- $user = Read-Host "Enter the login name of the desired user to being the off-boarding process"
- Get-UserToOffboard -User $User
- if ($Script:userExists -eq $false) {
- "$User does not exist, please re-run the script with the correct SamAccountName (Login Name)"
- }
- else {
- Start-Sleep -s 2
- "Please enter your email & password (required to connect to Office 365 services)"
- #Email credentials of user running the script
- $credentials = Get-Credential
- $scriptRunnerEmail = $credentials.UserName
- $scriptRunnerPassword = $credentials.Password
- $cred = New-Object System.Management.Automation.PSCredential -argumentlist $ScriptRunnerEmail, $ScriptRunnerPassword
- #Connect to O365 Modules
- #Please remove -Credential $cred if you get an error stating MFA verification is required on all three below
- Connect-MsolService -Credential $cred
- $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
- . "$MFAExchangeModule"
- Connect-EXOPSSession
- Connect-AzureAD -Credential $cred
- #Disable Active Directory account of targgeted user
- Invoke-DisableADAccount -User $User
- #Verify if account has been disabled
- $Script:ADAccountEnabled = Get-ADUser -Filter { SamAccountName -eq $User } | Select-Object -ExpandProperty "Enabled"
- #Block O365 Sign In
- Disable-SignIn -User $User
- #Email vendor to disable account on their end
- Send-Email -User $User -ScriptRunnerEmail $scriptRunnerEmail -ScriptRunnerPassword $scriptRunnerPassword -To ""
- }
- #Remove all license associated to the account
- try {
- (Get-MsolUser -UserPrincipalName $userEmail).licenses.AccountSkuId | ForEach-Object { Set-MsolUserLicense -UserPrincipalName $userEmail -RemoveLicenses $_ }
- }
- catch {
- "Unable to unlicense user, please do so manually"
- }
- #Convert To Shared Mailbox
- $userEmail = Get-ADUser $User -Properties * | Select-Object -ExpandProperty "UserPrincipalName"
- Set-Mailbox $userEmail -Type Shared
- #Remove EXOV2 Session
- Get-PSSession | Remove-PSSession
- "------------------------"
- "Task OVERVIEW FOR: $User"
- "OFFICE 365 SIGN-IN DISABLED: $Script:O365Disabled"
- "AD ACCOUNT ENABLED: $Script:ADAccountEnabled (False means account is DISABLED)"
- "HIDDEN FROM GAL: $Script:HiddenFromAddressList"
- "CONVERTED TO SHARED MAILBOX: $Script:ConvertedToSharedMailbox"
- "EMAIL SENT TO VENDOR: $Script:EmailSentToVendor"
- "-------------------------------------------------"
Advertisement
Add Comment
Please, Sign In to add comment