Hello,
Do you know how we can monitor AD users' expiration dates (not password expiration dates) through Zabbix.
Do you know how we can monitor AD users' expiration dates (not password expiration dates) through Zabbix.
Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties AccountExpirationDate | Select-Object Name, AccountExpirationDate | Export-Csv -Path "C:\chemin\vers\le\fichier\dates_expiration.csv" -NoTypeInformation
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the security group name
$securityGroupName = "Password Change Group Policy"
# Get the Distinguished Name (DN) of the security group
$securityGroupDN = (Get-ADGroup -Filter {Name -eq $securityGroupName}).DistinguishedName
# Get the Fine-Grained Password Policy linked to the specified security group
$fgpp = Get-ADFineGrainedPasswordPolicy -Filter {AppliesTo -eq $securityGroupDN}
# Check if a Fine-Grained Password Policy is linked to the group
if ($fgpp) {
# Retrieve the Maximum Password Age from Fine-Grained Password Policy and convert to days
$daysDuration = [math]::floor($fgpp.MaxPasswordAge.Days)
$epoch = New-Object DateTime 1970, 1, 1, 0, 0, 0, ([DateTimeKind]::Utc)
# Get all users from the specified security group
$users = Get-ADGroupMember -Identity $securityGroupDN | Get-ADUser -Properties EmailAddress, GivenName, PasswordLastSet, SamAccountName, DisplayName, LockedOut
# Create an array to store user objects
$userArray = @()
# Loop through each user and create a custom object with the desired attributes
foreach ($user in $users) {
$passwordLastSetUnixTimestamp = [math]::round(($user.PasswordLastSet - $epoch).TotalMilliseconds)
$expiryDate = $user.PasswordLastSet.AddDays($daysDuration)
$expiryDateFormatted = $expiryDate.ToString("dd MMMM yyyy")
$expiryDateDaysDifference = [math]::floor(($expiryDate.Date - (Get-Date).Date).TotalDays)
# Calculate "Expired" value based on today's date and ExpiryDateCalculated
$expired = if ($expiryDate -lt (Get-Date)) { '1' } else { '2' }
$userObject = [PSCustomObject]@{
EmailAddress = $user.EmailAddress -replace '"', '\"'
GivenName = $user.GivenName -replace '"', '\"'
PasswordLastSet = "$passwordLastSetUnixTimestamp"
SamAccountName = $user.SamAccountName -replace '"', '\"'
DisplayName = $user.DisplayName -replace '"', '\"'
LockedOut = "$($user.LockedOut)"
ExpiryDateCalculated = $expiryDateFormatted
DaysDifference = "$expiryDateDaysDifference"
Expired = "$expired"
}
# Add the user object to the array
$userArray += $userObject
}
# Manually handle square brackets based on the number of users
if ($userArray.Count -eq 1) {
Write-Host "["
Write-Output $userArray | ConvertTo-Json
Write-Host "]"
} elseif ($userArray.Count -gt 1) {
$userArray | ConvertTo-Json
}
} else {
Write-Host "No Fine-Grained Password Policy found for '$securityGroupName' or the group is not specified in any policy."
}
Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties AccountExpirationDate | Select-Object Name, AccountExpirationDate | Export-Csv -Path "C:\chemin\vers\le\fichier\dates_expiration.csv" -NoTypeInformation

Comment