Advertisement
iamkilo

Jenkins Termed Users

Mar 27th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $termlist = $env:termlist
  2. $accesslist = $env:accesslist
  3. $forward = $env:forward
  4. $365user = "blah@blah.org"
  5. $adUser = "Blah\jenkins-admin"
  6. $adPass = Get-Content c:\path\credentials.txt | ConvertTo-SecureString -AsPlainText -Force
  7. $365CRED = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $365user, $adPass
  8. $ADCRED = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adUser, $adPass
  9.  
  10. #Split the parameter by ; delimiter
  11.  $terms = $termlist -split ";"
  12.  
  13. #Get the AD users based on their UPN (e-mail), disable the account(s), move them to deactivated OU
  14.  foreach ($termee in $terms) {
  15.     $disabledUser = Get-ADUser -Filter "UserPrincipalName -eq '$termee'" -Credential $ADCRED
  16.     $folderName = $disabledUser.SamAccountName
  17.     Disable-ADAccount -Identity $disabledUser -Credential $ADCRED
  18.     Move-ADObject -Identity $disabledUser -TargetPath "OU=Deactivated Users,DC=contoso,DC=com" -Credential $ADCRED
  19.     Remove-Item -Path \\austin\user$\$folderName -Recurse -Force -Credential $ADCRED
  20.  }
  21.  
  22. #Establish remote PS session
  23. $SESSION = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $365CRED -Authentication Basic -AllowRedirection
  24. Import-PSSession $SESSION
  25. Connect-MsolService -Credential $365CRED
  26.  
  27. #Set Mailbox(es) to Shared instead of User
  28. foreach ($termee in $terms) {
  29. Get-Mailbox -identity $termee | set-mailbox -type Shared
  30.  
  31. #If -accesslist parameter is specified, add full access permissions to shared mailbox based on specified user(s)/group
  32. if($accesslist -ne $null) {
  33.     $access = $accesslist -split ";"
  34.     foreach($addAccess in $access) {
  35.         Add-MailboxPermission $termee -User $addAccess -AccessRights FullAccess
  36.     }
  37. }
  38.  
  39. #Pull down list of O365 licenses for a user, then remove them
  40. (get-MsolUser -UserPrincipalName $termee).licenses.AccountSkuId |
  41. foreach{
  42.     Set-MsolUserLicense -UserPrincipalName $termee -RemoveLicenses $_
  43. }
  44.  
  45. #Establish a 365 day litigation hold
  46. Set-Mailbox $termee -LitigationHoldEnabled $true -LitigationHoldDuration 365
  47.  
  48. #If -forward parameter is specified, forwards the email intended for the mailbox to that specified user
  49. if($forward -ne $null) {
  50.     Set-Mailbox $termee -ForwardingAddress $forward -DeliverToMailboxAndForward $False
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement