Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update InactiveUsersLast90Days.ps1 #4

Merged
merged 1 commit into from
Feb 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions InactiveUsersLast90Days.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import-module MSOnline

import-module MSOnline
#Let's get us an admin cred!
$userCredential = Get-Credential

#This connects to Azure Active Directory
#This connects to Azure Active Directory and passes admincreds
Connect-MsolService -Credential $userCredential
$ExoSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $userCredential -Authentication Basic -AllowRedirection
Import-PSSession $ExoSession

# Minus 90 Days from Today (Can Change to lower value)
$startDate = (Get-Date).AddDays(-90).ToString('MM/dd/yyyy')
#Todays current date
$endDate = (Get-Date).ToString('MM/dd/yyyy')

#Creates Array for Users
$allUsers = @()
#Uses Get-MsolUser cmdlet to get UPN
$allUsers = Get-MsolUser -All -EnabledFilter EnabledOnly | Select UserPrincipalName

#Creates another Array for dates
$loggedOnUsers = @()
$loggedOnUsers = Search-UnifiedAuditLog -StartDate $startDate -EndDate $endDate -Operations UserLoggedIn, PasswordLogonInitialAuthUsingPassword, UserLoginFailed -ResultSize 5000

$inactiveInLastThreeMonthsUsers = @()
$inactiveInLastThreeMonthsUsers = $allUsers.UserPrincipalName | where {$loggedOnUsers.UserIds -NotContains $_}

Write-Output "The following users have no logged in for the last 90 days:"
Write-Output $inactiveInLastThreeMonthsUsers

#Creates Array for UPNs not included.
$inactiveUsers = @()
$inactiveUsers = $allUsers.UserPrincipalName | where {$loggedOnUsers.UserIds -NotContains $_}
#Prints Results
Write-Output "The following users have not logged in for the last 90 days:"