Advertisement
Guest User

Untitled

a guest
Oct 31st, 2017
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .DESCRIPTION
  3. This script provides a standard off-boarding method for staff leaving
  4. the company.
  5.  
  6. The script does the following;
  7. 1. Disables the specified user account
  8. 2. Updates the user description with the user who disabled the account
  9. and the time/date when the account was disabled
  10. 3. Moves the account to the disabled user account OU (needs to exist)
  11. 5. Convert to a shared mailbox
  12. 6. Set Out Of Office
  13. 7. Revoke O365 Licenses
  14. 8. Give users rights on shared mailbox
  15.  
  16. Version 1.1 - 15/06/2017
  17. Initial release
  18. #>
  19.  
  20. $textboxResults = New-Object 'System.Windows.Forms.RichTextBox'
  21.  
  22.  
  23. Write-Host " **************** PLEASE ENTER ACTIVE DIRECTORY ADMIN CREDENTIALS **************** "
  24. $Credential = Get-Credential -Credential "$env:USERDOMAIN\"
  25. $DC = $env:LOGONSERVER.Substring(2)
  26.  
  27. #Initiate Remote PS Session to local DC
  28. $ADPowerShell = New-PSSession -ComputerName $DC -Authentication Negotiate -Credential $Credential
  29.  
  30. # Import-Module ActiveDirectory
  31. write-host "Importing Active Directory PowerShell Commandlets"
  32. Invoke-Command -Session $ADPowerShell -scriptblock { import-module ActiveDirectory }
  33. Import-PSSession -Session $ADPowerShell -Module ActiveDirectory -AllowClobber -ErrorAction Stop
  34.  
  35. # Retrieve AD Details
  36. $ADDetails = Get-ADDomain
  37. $Domain = $ADDetails.DNSRoot
  38. Clear-Host
  39.  
  40. write-host "Importing Office 365 PowerShell Commandlets"
  41. Write-Host -ForegroundColor White -BackgroundColor DarkBlue " **************** PLEASE ENTER OFFICE 365 ADMIN CREDENTIALS **************** "
  42. $Office365User = ([adsi]"LDAP://$(whoami /fqdn)").mail
  43. $Office365Credential = Get-Credential -Credential "$Office365User"
  44. $Office365PowerShell = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Office365Credential -Authentication Basic -AllowRedirection
  45. Import-PSSession $Office365PowerShell
  46. Clear-Host
  47.  
  48. write-host " **************** Disable Active Directory User Account & Enable Out Of Office **************** "
  49. write-host " "
  50.  
  51. # Get Variables
  52. $DisabledDate = Get-Date
  53. $LeaveDate = Get-Date -Format "dddd dd MMMM yyyy"
  54. $DisabledBy = Get-ADUser "$env:username" -properties Mail
  55. $DisabledByEmail = $DisabledBy.Mail
  56.  
  57. # Prompt for AD Username
  58. $Employee = Read-Host "Employee Username"
  59. $EmployeeDetails = Get-ADUser $Employee -properties *
  60. $GroupMembership = Get-ADUser $Employee -properties MemberOf
  61. If ($EmployeeDetails.Manager -ne $null)
  62. {
  63. $Manager = Get-ADUser $EmployeeDetails.Manager -Properties Mail
  64. }
  65.  
  66. # Prompt for Backup Colleague Username
  67. $BackUp = Read-Host "Backup Colleague Username"
  68. $BackUpDetails = Get-ADUser $BackUp -properties *
  69.  
  70. # Check which O365 License User has
  71. Connect-MsolService -Credential $Office365Credential
  72. $AccountSku = Get-MsolAccountSku
  73.  
  74. #$License = (Get-MSOLUser –UserPrincipalName $EmployeeDetails.mail).Licenses[0].AccountSkuID
  75. # E1 = "tennant:STANDARDPACK"
  76. # E3 = "tennant:ENTERPRISEPACK"
  77. #write-host $License
  78.  
  79. # Clear-Host
  80.  
  81. # Prompt for confirmation
  82. write-host " ******************************** CONFIRM USER DISABLE REQUEST ******************************** "
  83. write-host " "
  84. write-host -ForegroundColor Yellow "Please review the Employee details below to ensure you are disabling the correct user account."
  85. $EmployeeDetails | fl Name, Title, Company, @{ Expression = { $_.mail }; Label = "Email Address" }, @{Expression = { $_.Created }; Label = "Employment Started"} | Out-String
  86.  
  87. $choice = " "
  88. while ($choice -notmatch "[y|n]")
  89. {
  90. $choice = read-host "Do you want to continue? (Y/N)"
  91. }
  92.  
  93.  
  94. # Actions
  95. if ($choice -eq "y")
  96. {
  97. Clear-Host
  98. write-host " ******************************** DISABLING USER ACCOUNT ******************************** "
  99. write-host " "
  100. write-host "Step 1. Modifying user description for audit purposes" -ForegroundColor Yellow
  101. Set-ADUser $Employee -Description "Disabled by $($DisabledBy.name) on $DisabledDate"
  102.  
  103. write-host "Step 2. Disabling $Employee Active Directory Account." -ForegroundColor Yellow
  104. Disable-ADAccount $Employee
  105. write-host "Step 3. Moving $Employee to the Disabled User Accounts OU." -ForegroundColor Yellow
  106. Move-ADObject -Identity $EmployeeDetails.DistinguishedName -targetpath "OU="
  107.  
  108. write-host "Step 4. Removing $Employee from all Group memberships except Domain Users" -ForegroundColor Yellow
  109. write-host " "
  110. $GroupMembership.MemberOf | Remove-ADGroupMember -Member $Employee -Confirm:$false
  111.  
  112. write-host "Waiting 5 seconds for AD & Exchange OU update to complete"
  113.     sleep -Seconds 5
  114.  
  115. write-host " "
  116. write-host "Refreshing Employee Details for Exchange Modification."
  117. Get-ADUser $Employee -properties * | Format-List Name, Description
  118.  
  119. write-host "Step 5. Setting Exchange Out Of Office Auto-Responder." -ForegroundColor Yellow
  120. Set-MailboxAutoReplyConfiguration -Identity $EmployeeDetails.mail -AutoReplyState Enabled -InternalMessage " Put in Out Of Office here "                       
  121.                            
  122. Write-Host "Step 6. Convert to a shared mailbox"
  123. Set-Mailbox $EmployeeDetails.Mail -Type shared
  124.  
  125. Write-Host "Step 7. Revoke O365 Licenses"
  126. $LicenseDetails = (Get-MsolUser -UserPrincipalName $EmployeeDetails.mail).Licenses
  127. $LicenseDetails.Count;
  128. # If there's a license, show the details.
  129. # Otherwise, the output is blank.
  130. if ($LicenseDetails.Count -gt 0){
  131.   foreach ($i in $LicenseDetails){
  132.     #$i.AccountSkuId
  133.     if ($i.AccountSkuId -Match "tennant:VISIOCLIENT"){
  134.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:VISIOCLIENT"
  135.         }
  136.                     if ($i.AccountSkuId -Match "tennant:EMSPREMIUM"){
  137.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:EMSPREMIUM"  
  138.         }
  139.                      if ($i.AccountSkuId -Match "tennant:POWER_BI_PRO"){
  140.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:POWER_BI_PRO"
  141.         }
  142.                     if ($i.AccountSkuId -Match "tennant:ENTERPRISEPACK"){
  143.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:ENTERPRISEPACK"
  144.         }
  145.                     if ($i.AccountSkuId -Match "tennant:FLOW_FREE"){
  146.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:FLOW_FREE"
  147.         }
  148.                     if ($i.AccountSkuId -Match "tennant:POWERAPPS_VIRAL"){
  149.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:POWERAPPS_VIRAL"
  150.         }
  151.                     if ($i.AccountSkuId -Match "tennant:CRMSTANDARD"){
  152.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:CRMSTANDARD"
  153.         }
  154.                     if ($i.AccountSkuId -Match "tennant:POWER_BI_STANDARD"){
  155.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:POWER_BI_STANDARD"
  156.         }
  157.                     if ($i.AccountSkuId -Match "tennant:EMS"){
  158.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:EMS"
  159.         }
  160.                     if ($i.AccountSkuId -Match "tennant:CRMINSTANCE"){
  161.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:CRMINSTANCE"
  162.         }
  163.                     if ($i.AccountSkuId -Match "tennant:PROJECTPROFESSIONAL"){
  164.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:PROJECTPROFESSIONAL"
  165.         }
  166.                     if ($i.AccountSkuId -Match "tennant:DYN365_ENTERPRISE_TEAM_MEMBERS"){
  167.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:DYN365_ENTERPRISE_TEAM_MEMBERS"
  168.         }
  169.                     if ($i.AccountSkuId -Match "tennant:STANDARDPACK"){
  170.         Set-MsolUserLicense -UserPrincipalName $EmployeeDetails.Mail -RemoveLicenses "tennant:STANDARDPACK"
  171.         }
  172.     }
  173. }
  174.  
  175. Write-Host "Step 8. Give users rights on shared mailbox and forward e-mail"
  176. Write-Host "Write username for Full Access"
  177. Add-MailboxPermission $Employee -User $BackUp -AccessRights FullAccess
  178. Add-RecipientPermission $Employee -AccessRights SendAs -Trustee $Backup
  179. Set-Mailbox $EmployeeDetails.mail -ForwardingAddress $BackUpDetails.mail
  180. }else{
  181. write-host " "
  182. write-host "Employee disable request cancelled" -ForegroundColor Yellow
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement