Advertisement
jheinrichs79

Check for Disabled Users /w Licensed Mailboxes

Oct 29th, 2019
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Connect-EXOnline{
  2. param (
  3.         [string]$Credential
  4.     )
  5.  
  6. $credentials = Get-Credential -Credential $Credential
  7. Write-Output "Getting the Exchange Online cmdlets"
  8.  
  9. $Session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
  10. -ConfigurationName Microsoft.Exchange -Credential $credentials `
  11. -Authentication Basic -AllowRedirection
  12. Import-PSSession $Session
  13.  
  14. }
  15.  
  16. Function disconnect-EXOnline {
  17.     $session = Get-PSSession
  18.     foreach ($id in $session) {
  19.             if ($id.ConfigurationName -eq 'Microsoft.Exchange') {
  20.                     Remove-PSSession $session      
  21.         }
  22.     }
  23. }
  24.  
  25. Function Get-DisabledButLicensedMailboxes {
  26. param (
  27.         [string]$CSVFileLocation
  28.     )
  29.     $mailboxes = Get-Mailbox -ResultSize unlimited | Where-Object AccountDisabled -eq $True | Select-Object -Property Name,Alias,PrimarySmtpAddress,UserPrincipalName,AccountDisabled,Database,ProhibitSendQuota
  30.  
  31.     [array]$DisabledMailboxes = @()
  32.     foreach ($mailbox in $mailboxes){
  33.  
  34.         $LicenseInfo = Get-MsolUser -UserPrincipalName $mailbox.UserPrincipalName -ErrorAction SilentlyContinue | select-object DisplayName,Licenses,IsLicensed
  35.    
  36.         if ($LicenseInfo.IsLicensed -eq $True) {
  37.             $AccountSkuID = ($LicenseInfo.licenses).AccountSkuID
  38.             if (($AccountSkuID -like "*STANDARD*") -or ($AccountSkuID -like "*ENTERPRISE*")){
  39.                 $stats = Get-MailboxStatistics $mailbox.UserPrincipalName
  40.                 $DisabledButLicensed = New-Object -TypeName psobject
  41.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name Name -Value $mailbox.Name
  42.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name Alias -Value $mailbox.Alias
  43.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name PrimarySmtpAddress -Value $mailbox.PrimarySmtpAddress
  44.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name UserPrincipalName -Value $mailbox.UserPrincipalName
  45.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name AccountDisabled -Value $mailbox.AccountDisabled
  46.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name Database -Value $mailbox.Database
  47.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name ProhibitSendQuota -Value $mailbox.ProhibitSendQuota
  48.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name AccountSkuID -Value $AccountSkuID
  49.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name IsLicensed -Value $LicenseInfo.IsLicensed
  50.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name MailboxTypeDetail -Value $stats.MailboxTypeDetail
  51.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name TotalItemSize  -Value $stats.TotalItemSize
  52.                 $DisabledButLicensed | Add-Member -MemberType NoteProperty -Name LastUserActionTime -Value $stats.LastUserActionTime
  53.                 $DisabledMailboxes += $DisabledButLicensed
  54.             }
  55.         }
  56.     }
  57.     $DisabledMailboxes | Export-Csv $CSVFileLocation -NoTypeInformation
  58.     $DisabledMailboxes | Out-GridView
  59. }
  60. #Change these items to fit your domain and email address.
  61. $emailAddress = "address@domain.ca"
  62. $csv = "C:\bin\Company-disabled-Mailboxes.csv"
  63.  
  64. Connect-EXOnline -Credential $emailAddress
  65. Connect-MsolService
  66.  
  67. Get-DisabledButLicensedMailboxes -CSVFileLocation $csv
  68.  
  69. Disconnect-EXOnline
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement