ShiftNick

Audit-InactiveMailboxes

Jun 21st, 2016
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################
  2.  
  3. #Get current date
  4. $date = Get-date -Format MMM-dd-yyyy
  5.  
  6. #Number of days to substract from current date
  7. $num = '-30'
  8.  
  9. #Set date to check last logon time
  10. $lltdate = (Get-Date).AddDays($num)
  11.  
  12. #Find mailboxes with lastlogontime less than date provided, do not show null results and export to a CSV
  13. Get-Mailbox -ResultSize unlimited | Get-MailboxStatistics | where {($_.lastlogontime -lt $lltdate) -and ($_.lastlogontime -ne $null)} | select DisplayName,lastlogontime | Export-Csv -Path C:\temp\MailboxAudit_$num-days_$date.csv -NoTypeInformation -Append
  14.  
  15.  
  16. ###########################
  17.  
  18.  
  19. #Import CSV of display names from previous step
  20. $displaynames = Import-Csv -Path C:\temp\MailboxAudit_$num-days_$date.csv -Delimiter ','
  21.  
  22. foreach($displayname in $displaynames){
  23.    
  24.     #create new variable with for the name attribute filter
  25.     $name  = "$($displayname.displayname)"
  26.  
  27.     #Search AD for accounts that match the filter criteria and export to csv
  28.     Get-ADUser -Filter {(name -eq $name)} | select @{n="name";e={$($displayname.DisplayName)}},@{n="LastLogonTime";e={$($displayname.lastlogontime)}},samaccountname,userprincipalname,enabled |Export-Csv -Path c:\temp\MailAudit_$num-days_$date-Clean.csv -Append -NoTypeInformation
  29.  
  30. }
  31.  
  32.  
  33. ###########################
  34.  
  35.  
  36. #Import CSV of user returned from previous search loop
  37. $UPNs = Import-Csv -Delimiter ',' -Path C:\temp\MailAudit_$num-days_$date-Clean.csv
  38.  
  39. foreach($upn in $upns){
  40.    
  41.     #Query Office365 and only return users that are showing as licensed. Out to a text file for further action
  42.     Get-MsolUser -UserPrincipalName $upn.UserPrincipalName | where {$_.islicensed -eq $true} | select -ExpandProperty userprincipalname|  Out-File -FilePath c:\temp\MailboxAudit_UPNS-Licensed_$date.txt -Append
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment