Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
1,275
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. 4. Sets an out of office reply stating that the employee has left the company
  12. 5. Convert to a shared mailbox
  13. 6. Revoke O365 Licenses
  14. 7. Give users rights on shared mailbox
  15. 8. Hides the mail account from the Global Adress List
  16.  
  17. Version 1.0
  18. Initial release
  19. #>
  20.  
  21. Write-Host " **************** PLEASE ENTER ACTIVE DIRECTORY ADMIN CREDENTIALS **************** "
  22. $Credential = Get-Credential -Credential "$env:USERDOMAIN\$env:USERNAME"
  23. $DC = $env:LOGONSERVER.Substring(2)
  24.  
  25. #Initiate Remote PS Session to local DC
  26. $ADPowerShell = New-PSSession -ComputerName $DC -Authentication Negotiate -Credential $Credential
  27.  
  28. # Import-Module ActiveDirectory
  29. write-host "Importing Active Directory PowerShell Commandlets"
  30. Invoke-Command -Session $ADPowerShell -scriptblock { import-module ActiveDirectory }
  31. Import-PSSession -Session $ADPowerShell -Module ActiveDirectory -AllowClobber -ErrorAction Stop
  32.  
  33. # Retrieve AD Details
  34. $ADDetails = Get-ADDomain
  35. $Domain = $ADDetails.DNSRoot
  36. Clear-Host
  37.  
  38. write-host "Importing Office 365 PowerShell Commandlets"
  39. Write-Host -ForegroundColor White -BackgroundColor DarkBlue " **************** PLEASE ENTER OFFICE 365 ADMIN CREDENTIALS **************** "
  40. $Office365Credential = Get-Credential
  41. $Office365PowerShell = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Office365Credential -Authentication Basic -AllowRedirection
  42. Import-PSSession $Office365PowerShell
  43. Clear-Host
  44.  
  45. write-host " **************** Disable Active Directory User Account & Enable Out Of Office **************** "
  46. write-host " "
  47.  
  48. # Get Variables
  49. $DisabledDate = Get-Date
  50. $LeaveDate = Get-Date -Format "dddd dd MMMM yyyy"
  51. $DisabledBy = Get-ADUser "$env:username" -properties Mail
  52. $DisabledByEmail = $DisabledBy.Mail
  53.  
  54. # Prompt for AD Username
  55. $Employee = Read-Host "Employee Username"
  56. $EmployeeDetails = Get-ADUser $Employee -properties *
  57. If ($EmployeeDetails.Manager -ne $null)
  58. {
  59. $Manager = Get-ADUser $EmployeeDetails.Manager -Properties Mail
  60. }
  61.  
  62. # Prompt for Backup Colleague Username
  63. $BackUp = Read-Host "Backup Colleague Username"
  64. $BackUpDetails = Get-ADUser $BackUp -properties *
  65.  
  66. # Check which O365 License User has
  67. Connect-MsolService -Credential $Office365Credential
  68. $License = (Get-MSOLUser –UserPrincipalName $EmployeeDetails.mail).Licenses[0].AccountSkuID
  69. # E1 = "<tenant>:STANDARDPACK"
  70. # E3 = "<tenant>:ENTERPRISEPACK"
  71. Clear-Host
  72.  
  73. # Prompt for confirmation
  74. write-host " ******************************** CONFIRM USER DISABLE REQUEST ******************************** "
  75. write-host " "
  76. write-host -ForegroundColor Yellow "Please review the Employee details below to ensure you are disabling the correct user account."
  77. $EmployeeDetails | fl Name, Title, Company, @{ Expression = { $_.mail }; Label = "Email Address" }, @{Expression = { $_.Created }; Label = "Employment Started"}
  78.  
  79. $choice = " "
  80. while ($choice -notmatch "[y|n]")
  81. {
  82. $choice = read-host "Do you want to continue? (Y/N)"
  83. }
  84.  
  85. # Actions
  86. if ($choice -eq "y")
  87. {
  88. Clear-Host
  89. write-host " ******************************** DISABLING USER ACCOUNT ******************************** "
  90. write-host " "
  91. write-host "Step1. Modifying user description for audit purposes" -ForegroundColor Yellow
  92. Set-ADUser $Employee -Description "Disabled by $($DisabledBy.name) on $DisabledDate"
  93. write-host "Step2. Disabling $Employee Active Directory Account." -ForegroundColor Yellow
  94. Disable-ADAccount $Employee
  95. write-host "Step3. Moving $Employee to the Disabled User Accounts OU." -ForegroundColor Yellow
  96. write-host " "
  97. Move-ADObject -Identity $EmployeeDetails.DistinguishedName -targetpath "OU=Template,OU=Users,DC=contoso,DC=com"
  98. write-host "Waiting 5 seconds for AD & Exchange OU update to complete"
  99.     sleep -Seconds 5
  100.    
  101. write-host " "
  102. write-host "Refreshing Employee Details for Exchange Modification."
  103. write-host " "
  104. Get-ADUser $Employee -Properties Description | Format-List Name, Enabled, Description
  105. write-host "Step 4. Setting Exchange Out Of Office Auto-Responder." -ForegroundColor Yellow
  106.     Set-MailboxAutoReplyConfiguration -Identity $Employee.Mail -AutoReplyState enabled -ExternalAudience all -InternalMessage "Dear, I have left $($EmployeeDetails.company) since $LeaveDate. You can contact my colleague $($BackUpDetails.name) via e-mail: $($BackUpDetails.mail) or telephone: $($BackUpDetails.officephone)." -ExternalMessage "Dear, I have left $($EmployeeDetails.company) since $LeaveDate. You can contact my colleague $($BackUpDetails.name) via e-mail: $($BackUpDetails.mail) or telephone: $($BackUpDetails.officephone)."
  107. Write-Host "Step 5. Convert to a shared mailbox"
  108. Set-Mailbox $EmployeeDetails.Mail -Type shared
  109. Write-Host "Step 6. Revoke O365 Licenses"
  110. if ($License -Match "<tenant>:STANDARDPACK")
  111. {
  112.     Set-MsolUserLicense -Identity $EmployeeDetails.Mail -RemoveLicenses "<tenant>:STANDARDPACK"
  113. }
  114. Else
  115. {
  116.     Set-MsolUserLicense -Identity $EmployeeDetails.Mail -RemoveLicenses "<tenant>:ENTERPRISEPACK"
  117. }
  118. Write-Host "Step 7. Give users rights on shared mailbox"
  119. Write-Host "Write username for Full Access"
  120. Add-MailboxPermission $EmployeeDetails.Mail -User $BackUp -AccessRights FullAccess
  121. Write-Host "Step 8. Hiding $($EmployeeDetails.name) from Global Address lists" -ForegroundColor Yellow
  122. Set-ADUser -identity $Employee -add @{ msExchHideFromAddressLists = "True" }
  123. Set-ADUser -instance $EmployeeDetails -whatif
  124. }else{
  125. write-host " "
  126. write-host "Employee disable request cancelled" -ForegroundColor Yellow
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement