03-12-2024 08:00 AM
Hi everybody,
I'd like to create a script using the Get-JCUser command to export all users with password_expiration_date minor than XX days or, maybe, with password_expiration_date - today() < XX days.
May I have some help from you ?
Thanks in advance,
Dario
Solved! Go to Solution.
03-16-2024 05:31 AM
Hello @dariovernelli ,
I hope it works for you.
Connect-JCOnline YourAPI
# Put a csv with users in C:\Temp\users.csv Column name should be "username"
$csvPath = "C:\Temp\users.csv"
$exportedUsers = @()
$users = Import-Csv -Path $csvPath
foreach ($user in $users) {
$displayName = $user.username
Write-Host "Username: $displayName"
# Get the password_date
$passwordDate = Get-JCUser $displayName | Select-Object -ExpandProperty password_date
# Convert the date to the appropriate format
$passwordDateTime = [DateTime]::ParseExact($passwordDate, "yyyy-MM-ddTHH:mm:ss.fffZ", [System.Globalization.CultureInfo]::InvariantCulture)
# Check the password change date
$daysSincePasswordChange = (Get-Date) - $passwordDateTime
# If the password change date is more than 100 days ago
if ($daysSincePasswordChange.TotalDays -gt 100) {
Write-Host "The user has to change your password."
$exportedUsers += New-Object PSObject -Property @{
"Username" = $displayName
"Password Date" = $passwordDate
"Password Status" = "The user has to change your password"
}
} else {
Write-Host "There is no problem."
$exportedUsers += New-Object PSObject -Property @{
"Username" = $displayName
"Password Date" = $passwordDate
"Password Status" = "There is no problem"
}
}
}
# Export the results
$exportedUsers | Export-Csv -Path "C:\Temp\exported_users.csv" -NoTypeInformation
03-14-2024 02:39 PM
Hi @dariovernelli give me a day or two to try and put this together 🙂
03-16-2024 05:31 AM
Hello @dariovernelli ,
I hope it works for you.
Connect-JCOnline YourAPI
# Put a csv with users in C:\Temp\users.csv Column name should be "username"
$csvPath = "C:\Temp\users.csv"
$exportedUsers = @()
$users = Import-Csv -Path $csvPath
foreach ($user in $users) {
$displayName = $user.username
Write-Host "Username: $displayName"
# Get the password_date
$passwordDate = Get-JCUser $displayName | Select-Object -ExpandProperty password_date
# Convert the date to the appropriate format
$passwordDateTime = [DateTime]::ParseExact($passwordDate, "yyyy-MM-ddTHH:mm:ss.fffZ", [System.Globalization.CultureInfo]::InvariantCulture)
# Check the password change date
$daysSincePasswordChange = (Get-Date) - $passwordDateTime
# If the password change date is more than 100 days ago
if ($daysSincePasswordChange.TotalDays -gt 100) {
Write-Host "The user has to change your password."
$exportedUsers += New-Object PSObject -Property @{
"Username" = $displayName
"Password Date" = $passwordDate
"Password Status" = "The user has to change your password"
}
} else {
Write-Host "There is no problem."
$exportedUsers += New-Object PSObject -Property @{
"Username" = $displayName
"Password Date" = $passwordDate
"Password Status" = "There is no problem"
}
}
}
# Export the results
$exportedUsers | Export-Csv -Path "C:\Temp\exported_users.csv" -NoTypeInformation
05-21-2024 04:43 AM
Hi Yabagay,
sorry for my late response. It works well on Windows OS, many thanks.
I have only changed the line 15 to:
05-21-2024 04:59 AM
Hello Dario,
Thank you for updating it.
Glad to hear it.
Yusuf
New to the site? Take a look at these additional resources:
Ready to join us? You can register here.