Guest User

Untitled

a guest
Oct 16th, 2020
3,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $Script:userExists
  2. $Script:azureADConnected = $false
  3. $Script:O365Disabled = $false
  4. $Script:ADAccountEnabled = $true
  5. $Script:HiddenFromAddressList = $false
  6. $Script:ConnectedToO365 = $false
  7. $Script:EmailSentToVendor = $false
  8. $Script:ConvertedToSharedMailbox = $false
  9.  
  10. function Prerequisites {
  11.    
  12.     "VERIFYING O365 MODULES"
  13.  
  14.     if(-not(Get-Module -ListAvailable -Name "MSONLINE"))
  15.     {
  16.         Write-Warning "MSONLINE MODULE DOES NOT EXIST"
  17.         "ATTEMPTING TO INSTALL MODULE"
  18.         try{
  19.             Install-Module -Name MSONLINE
  20.         }
  21.         catch{
  22.             "Failed to install module MSONLINE"
  23.             Exit
  24.         }
  25.     }
  26.  
  27.     if(-not(Get-Module -ListAvailable -Name "ExchangeOnlineManagement"))
  28.     {
  29.         Write-Warning "ExchangeOnlineManagement MODULE DOES NOT EXIST"
  30.         "ATTEMPTING TO INSTALL MODULE"
  31.         try{
  32.             Install-Module -Name ExchangeOnlineManagement
  33.         }
  34.         catch{
  35.             "Failed to install module ExchangeOnlineManagement"
  36.             Exit
  37.         }
  38.     }
  39.  
  40.     if(-not(Get-Module -ListAvailable -Name "AzureAD"))
  41.     {
  42.         Write-Warning "AzureAD MODULE DOES NOT EXIST"
  43.         "ATTEMPTING TO INSTALL MODULE"
  44.         try{
  45.             Install-Module -Name AzureAD
  46.         }
  47.         catch{
  48.             "Failed to install module AzureAD"
  49.             Exit
  50.         }
  51.     }
  52.     "All MODULES PRESENT"
  53.  
  54. }
  55.  
  56. function Send-Email {
  57.     param (
  58.         [string]
  59.         $User,
  60.         $ScriptRunnerEmail,
  61.         $ScriptRunnerPassword,
  62.         $To
  63.  
  64.     )
  65.  
  66.     $cred = New-Object System.Management.Automation.PSCredential -argumentlist $ScriptRunnerEmail, $ScriptRunnerPassword
  67.     $body = ""
  68.  
  69.    
  70.     try {
  71.         Send-MailMessage -From $ScriptRunnerEmail -To $To -Cc "" -Subject "" -Body $body -SmtpServer "smtp.office365.com" -Port "587" -UseSsl -Credential $cred
  72.         $Script:EmailSentToVendor = $true
  73.     }
  74.     catch {
  75.         "Unable to send an email to vendor, send one manually to have the account disabled"
  76.     }
  77.  
  78. }
  79.    
  80. function Disable-SignIn {
  81.     param (
  82.         [string]
  83.         $User
  84.     )
  85.    
  86.     #email of user to disable    
  87.     $upn = Get-ADUser -Identity $User -Properties * | Select-Object -ExpandProperty "UserPrincipalName"
  88.    
  89.     Set-AzureADUser -ObjectId $upn -AccountEnabled $false
  90.     $isEnabled = Get-AzureADUser -ObjectId $upn | Select-Object -ExpandProperty AccountEnabled
  91.    
  92.     if ($isEnabled -eq $true) {
  93.         "Unable to disable AD account: $upn"
  94.         "Script has terminated"
  95.         exit
  96.     }
  97.  
  98.     #Check if sign in is blocked (O365)
  99.     $Script:O365Disabled = (Get-MsolUser -UserPrincipalName $upn).blockcredential
  100.    
  101.     #Hide from GAL
  102.     Set-ADUser -Identity $User -Replace @{msExchHideFromAddressLists = "TRUE" }
  103.     #Verify attribute above was applied
  104.     $Script:HiddenFromAddressList = Get-ADUser -Identity $User -Properties * | Select-Object -ExpandProperty msExchHideFromAddressLists
  105.  
  106.      
  107. }
  108.  
  109. function Get-UserToOffboard {
  110.     param (
  111.         [string]
  112.         $User
  113.     )
  114.    
  115.     #Check if account exists in Active Directory
  116.     if (@(Get-ADUser -Filter { SamAccountName -ne $User }).Count -eq 0) {
  117.         $Script:userExists = $true
  118.     }
  119. }
  120.  
  121. function Invoke-DisableADAccount {
  122.     param (
  123.         [string]
  124.         $User
  125.     )
  126.     Set-ADUser -Identity $User -Enabled $false
  127. }
  128.  
  129. #Check if all Exchange Modules are available and will attempt to install, will terminate script if any of the modules aren't present
  130. Prerequisites
  131.  
  132. $scriptUser = $env:USERNAME
  133. "Welcome $scriptUser"
  134. $user = Read-Host "Enter the login name of the desired user to being the off-boarding process"
  135. Get-UserToOffboard -User $User
  136.  
  137. if ($Script:userExists -eq $false) {
  138.     "$User does not exist, please re-run the script with the correct SamAccountName (Login Name)"
  139. }
  140. else {
  141.  
  142.     Start-Sleep -s 2
  143.  
  144.     "Please enter your email & password (required to connect to Office 365 services)"
  145.  
  146.     #Email credentials of user running the script
  147.     $credentials = Get-Credential
  148.     $scriptRunnerEmail = $credentials.UserName
  149.     $scriptRunnerPassword = $credentials.Password
  150.     $cred = New-Object System.Management.Automation.PSCredential -argumentlist $ScriptRunnerEmail, $ScriptRunnerPassword
  151.    
  152.     #Connect to O365 Modules
  153.     #Please remove -Credential $cred if you get an error stating MFA verification is required on all three below
  154.    
  155.     Connect-MsolService -Credential $cred
  156.     $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
  157.     . "$MFAExchangeModule"
  158.     Connect-EXOPSSession
  159.     Connect-AzureAD -Credential $cred
  160.    
  161.     #Disable Active Directory account of targgeted user
  162.     Invoke-DisableADAccount -User $User
  163.     #Verify if account has been disabled
  164.     $Script:ADAccountEnabled = Get-ADUser -Filter { SamAccountName -eq $User } | Select-Object -ExpandProperty "Enabled"
  165.     #Block O365 Sign In
  166.     Disable-SignIn -User $User
  167.     #Email vendor to disable account on their end
  168.     Send-Email -User $User -ScriptRunnerEmail $scriptRunnerEmail -ScriptRunnerPassword $scriptRunnerPassword -To ""
  169.    
  170.    
  171.  
  172. }
  173. #Remove all license associated to the account
  174. try {
  175.     (Get-MsolUser -UserPrincipalName $userEmail).licenses.AccountSkuId | ForEach-Object { Set-MsolUserLicense -UserPrincipalName $userEmail -RemoveLicenses $_ }
  176. }
  177. catch {
  178.     "Unable to unlicense user, please do so manually"
  179. }
  180.  
  181. #Convert To Shared Mailbox
  182. $userEmail = Get-ADUser $User -Properties * | Select-Object -ExpandProperty "UserPrincipalName"
  183. Set-Mailbox $userEmail -Type Shared
  184.  
  185. #Remove EXOV2 Session
  186. Get-PSSession | Remove-PSSession  
  187.  
  188.  
  189. "------------------------"
  190. "Task OVERVIEW FOR: $User"
  191. "OFFICE 365 SIGN-IN DISABLED: $Script:O365Disabled"
  192. "AD ACCOUNT ENABLED: $Script:ADAccountEnabled (False means account is DISABLED)"
  193. "HIDDEN FROM GAL: $Script:HiddenFromAddressList"
  194. "CONVERTED TO SHARED MAILBOX: $Script:ConvertedToSharedMailbox"
  195. "EMAIL SENT TO VENDOR: $Script:EmailSentToVendor"
  196. "-------------------------------------------------"
  197.  
Advertisement
Add Comment
Please, Sign In to add comment