Advertisement
JustinCooper

Untitled

Oct 1st, 2020
1,549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [cmdletbinding()]
  2. param (
  3.  [switch]$Log,
  4.  [Alias('wi')]
  5.  [switch]$WhatIf
  6. )
  7. Clear-Host; $error.clear()
  8.  
  9. try { Stop-Transcript } catch { Write-Verbose 'No Transcript Started' }
  10. Write-Host $MyInvocation.MyCommand.Name -ForegroundColor Green
  11.  
  12. # ==== Begin logging =====
  13. $LogPath = '.\logs\' + $(if ($WhatIf) { 'TEST-' }) + $MyInvocation.MyCommand.Name + $(get-date -f -20y.M.d-hhmmss) + ".log"
  14. if ($Log) { Start-Transcript $LogPath }
  15.  
  16. Connect-MsolService -Credential (Get-Credential -Message 'An account with the proper rights')
  17.  
  18. 'Getting staff AD objects with mail attribute'
  19. $staffADObjs = Get-ADUser -Filter {(mail -like "*@*.org") -and (employeeid -like "*")} -Properties lastlogondate,employeeid,mail,title
  20.  
  21. $A3types = 'Manager','IT Staff','Office Assistant','Jobs that need more licensing power'
  22.  
  23. foreach ($adObj in $staffADObjs){
  24.  if ($WhatIf) { '=======================================================================' }
  25.  Write-Debug ( '{0} LastLogon {1} - Process?' -f $adObj.samaccountname, $adObj.LastLogonDate )
  26.  
  27.  if ($A3Types -contains $adObj.title) {
  28.   $A3 = $true
  29.   $targetLicense = 'YourOrgName:M365EDU_A3_FACULTY'
  30.  }
  31.  else { $targetLicense = 'YourOrgName:STANDARDWOFFPACK_FACULTY' }
  32.  
  33.  $msolUser = Get-MsolUser -UserPrincipalName $adObj.UserPrincipalName -ErrorAction SilentlyContinue
  34.  
  35.  if (!$msolUser) {
  36.   ('{0}, MSOL No such user.' -f $adObj.UserPrincipalName)
  37.   continue
  38.  }
  39.  
  40.  if ($adObj.DistinguishedName -like "*Resignation-Termination-Retired,OU=Disabled_User_Objects*"){ # Begin Retired Check
  41.   foreach ($license in ($msolUser.Licenses.AccountSkuId)){
  42.    add-log retired ('{0},removing license: {1}' -f $adObj.samaccountname, $license) -WhatIf:$WhatIf
  43.    if (!$WhatIf) { Set-MsolUserLicense -UserPrincipalName $adObj.UserPrincipalName -RemoveLicenses $license }
  44.   }
  45.  } # End Retired Check
  46.  else {
  47.   $cutOffDate = (Get-Date).AddMonths(-6)
  48.   # Staleness Check
  49.   # Give likely inactive accounts the default (A1) license
  50.   if ($adObj.LastLogonDate -le $cutOffDate) {
  51.    $targetLicense = 'YourOrgName:STANDARDWOFFPACK_FACULTY'
  52.    ('{0},{1},Possible Stale account detected. Changing Target License: {2}' -f $adObj.samaccountname, $adObj.LastLogonDate,$targetLicense )
  53.   }
  54.  
  55.   Write-Verbose ($msolUser.Licenses.AccountSkuId | Out-String)
  56.  
  57.   if ($msolUser.Licenses.AccountSkuId -contains $targetLicense){
  58.    ('{0},Proper license present: {1}' -f $adObj.samaccountname, $targetLicense)
  59.   } else {
  60.    foreach ($license in ($msolUser.Licenses.AccountSkuId)){ # Begin Bad License Check
  61.  
  62.     $badLicenseTypes = 'YourOrgName:EXCHANGESTANDARD_FACULTY','YourOrgName:STANDARDWOFFPACK_IW_FACULTY',
  63.      'YourOrgName:STANDARDWOFFPACK_IW_STUDENT','YourOrgName:STANDARDWOFFPACK_STUDENT'
  64.     # Active Accounts that are assigned A3 licenses that need to be switched over from the default
  65.     if ($A3 -and ($adObj.LastLogonDate -ge $cutOffDate)) { $badLicenseTypes += 'YourOrgName:STANDARDWOFFPACK_FACULTY' }
  66.     if ( $badLicenseTypes -contains $license ){
  67.      ('{0},{1}' -f $adObj.samaccountname, $license )
  68.      if (!$WhatIf) { Set-MsolUserLicense -UserPrincipalName $adObj.UserPrincipalName -RemoveLicenses $license  }
  69.     }
  70.    } # End Bad License Check
  71.  
  72.    # Assign proper licence type
  73.    ('{0},{1}' -f $adObj.samaccountname, $targetLicense )
  74.    if (!$WhatIf) {
  75.     Set-MsolUserLicense -UserPrincipalName $adObj.UserPrincipalName -AddLicenses $targetLicense
  76.     foreach ($lic in ((Get-MsolUser -UserPrincipalName $adObj.UserPrincipalName).Licenses.AccountSkuId)) {
  77.      ( '{0},{1}' -f $adObj.UserPrincipalName, $lic )
  78.     }
  79.    }
  80.   }
  81.  }
  82. }
  83.  
  84. # End Logging
  85. if ($Log) { Stop-Transcript }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement