Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. Import-Module ActiveDirectory
  2. Import-Module MSOnline
  3.  
  4. $password = ConvertTo-SecureString 'PASSWORD' -AsPlainText -Force
  5. $LiveCred = New-Object System.Management.Automation.PSCredential ("username@domain.com", $password)
  6. New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
  7.  
  8. Connect-MsolService -Credential $Livecred
  9.  
  10.  
  11.  
  12. $userID = Import-Csv "c:exportlist.csv"
  13. $LogFile = "C:exportlogs.txt"
  14. foreach ($user in $userID)
  15. {
  16.  
  17. $ADuser = Get-ADUser -Filter "EmployeeId -eq $($user.EmployeeID)" -Properties whenCreated, Enabled, SAMAccountName
  18. $O365User = Get-MsolUser -UserPrincipalName $ADuser.UserPrincipalName
  19.  
  20. if (($ADuser.Enabled -eq $true) -and ($O365User.isLicensed -eq $true))
  21. {
  22. Get-MsolUSer -UserPrincipalName $ADuser.UserPrincipalName
  23. Set-MsolUserLicense -UserPrincipalName $ADuser.UserPrincipalName -RemoveLicenses "company:ENTERPRISEPACK"
  24. #move user OU, change description, disable account, remove SG members list
  25. #$Date = Get-Date -Format MM-dd-yyyy
  26. #Set-ADUser -Identity $ADuser.SAMAccountName -Replace @{info="User disabled at $Date"}
  27. # $User has been disabled and remove office 365 licence from user.
  28. #"$ADuser.SAMAccountName has been disabled and remove office 365 licence from user." | Out-File $LogFile -Append -Force
  29.  
  30. }
  31. elseif (($ADuser.Enabled -eq $true) -and ($O365User.isLicensed -eq $false))
  32. {
  33. #move user OU, change description, disable account, remove SG members list
  34. #Set-ADUser username -Replace @{info='New info for the notes field'}
  35. #"$ADuser.SAMAccountName has been disabled and already without office 365 licence." | Out-File $LogFile -Append -Force
  36.  
  37. }
  38. elseif (($ADuser.Enabled -eq $false) -and ($O365User.isLicensed -eq $false))
  39. {
  40. #do nothing
  41. #"$ADuser.SAMAccountName has been already disabled and already without office 365 licence." | Out-File $LogFile -Append -Force
  42.  
  43. }
  44. elseif (($ADuser.Enabled -eq $false) -and ($O365User.isLicensed -eq $true))
  45. {
  46. Get-MsolUSer -UserPrincipalName $ADuser.UserPrincipalName
  47. Set-MsolUserLicense -UserPrincipalName $ADuser.UserPrincipalName -RemoveLicenses "company:ENTERPRISEPACK"
  48. #"$ADuser.SAMAccountName has been already disabled and but revoke office 365 licence." | Out-File $LogFile -Append -Force
  49.  
  50. }
  51. else
  52. {
  53. #User does not exist in AD
  54. #"$user.EmployeeID does not exist in Active Directory." | Out-File $LogFile -Append -Force
  55.  
  56. }
  57.  
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement