# Author: Elif Uçhan
# Date: 07/29/2024
# Description: Bu betik, Active Directory'de son 90 gün içinde oturum açmamış olan etkin kullanıcıları bulur
# ve bu kullanıcıların ayrıntılarını CSV dosyasına yazar.
$day = 90
$date = (Get-Date).AddDays(-$day)
# İnaktif kullanıcıları bulma
$inactiveUsers = Get-ADUser -Filter {LastLogonDate -lt $date -and Enabled -eq $true} -Properties LastLogonDate, DistinguishedName, emailaddress |
Select-Object Name, SamAccountName, LastLogonDate, DistinguishedName, emailaddress |
Sort-Object LastLogonDate -Descending
# İnaktif oldukları gün sayısını belirle
function DaysInactive ([datetime]$LastLogonDate) {
return ((Get-Date) - $LastLogonDate).Days
}
# OU bilgilerini almak için fonksiyon
function Get-OU([string]$DistinguishedName) {
$ouParts = $DistinguishedName -split ',' | Where-Object { $_ -like "OU=*" }
$ou = $ouParts -join ', ' # OU bilgisini oluştur
return $ou
}
# Kullanıcı verilerini işleyip CSV dosyasına yazma
$results = foreach ($user in $inactiveUsers) {
$daysInactive = DaysInactive -LastLogonDate $user.LastLogonDate
$ou = Get-OU-DistinguishedName $user.DistinguishedName
[PSCustomObject]@{
Name = $user.Name
SamAccountName = $user.SamAccountName
LastLogonDate = $user.LastLogonDate
DaysInactive = $daysInactive
OrganizationalUnit = $ou
Mail = $user.emailaddress
}
}
# Sonuçları CSV dosyasına aktar
$results | Export-Csv -Path "C:Scriptsinactiveusers.csv" -NoTypeInformation -Encoding UTF8