Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################
- #Get current date
- $date = Get-date -Format MMM-dd-yyyy
- #Number of days to substract from current date
- $num = '-30'
- #Set date to check last logon time
- $lltdate = (Get-Date).AddDays($num)
- #Find mailboxes with lastlogontime less than date provided, do not show null results and export to a CSV
- 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
- ###########################
- #Import CSV of display names from previous step
- $displaynames = Import-Csv -Path C:\temp\MailboxAudit_$num-days_$date.csv -Delimiter ','
- foreach($displayname in $displaynames){
- #create new variable with for the name attribute filter
- $name = "$($displayname.displayname)"
- #Search AD for accounts that match the filter criteria and export to csv
- 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
- }
- ###########################
- #Import CSV of user returned from previous search loop
- $UPNs = Import-Csv -Delimiter ',' -Path C:\temp\MailAudit_$num-days_$date-Clean.csv
- foreach($upn in $upns){
- #Query Office365 and only return users that are showing as licensed. Out to a text file for further action
- Get-MsolUser -UserPrincipalName $upn.UserPrincipalName | where {$_.islicensed -eq $true} | select -ExpandProperty userprincipalname| Out-File -FilePath c:\temp\MailboxAudit_UPNS-Licensed_$date.txt -Append
- }
Advertisement
Add Comment
Please, Sign In to add comment