help returning a simple result...

New here, so go easy on me, I am trying to create a simple script that shows an ad users password expiration date. In the first If statement, I need to kick back the users SamAccountName if it was looked up by email. Can someone help?

 

 

$Answer = Read-host -Prompt “Do you know username?(y/n)”
if ($Answer = ‘n’){
$User = Read-host -Prompt “Enter users email for lookup”
Get-ADUser -Filter {EmailAddress -eq ‘$User’} | select SamAccountName

$username = Read-host -Prompt “What is the username to check?”
}
else {
$username = Read-host -Prompt “What is the username to check?”
}
$usr = Get-ADUser $username –Properties “DisplayName”, “msDS-UserPasswordExpiryTimeComputed”

$exprFileDate = $usr.“msDS-UserPasswordExpiryTimeComputed”

if ($exprFileDate -eq [int64]::MaxValue) {

Max int64 value indicates no expiration

This is too large to convert to datetime so add 99999 instead

$exprDate = (Get-Date).AddDays(99999)
$exprDays = 99999
} else {
$exprDate = [datetime]::FromFileTime($exprFileDate)
$exprDays = [Math]::max(0, [int]($exprDate - (Get-Date)).TotalDays)
}

[pscustomobject]@{
Displayname = $usr.Displayname
ExpiryDate = $exprDate
DaysRemaining = $exprDays
}

You want to show the samaccountname and then turn around and ask them to input it? Seems counter intuitive. The filter parameter should be in a string format. Perhaps this below is more like what you’re wanting.

$Answer = Read-host -Prompt “Do you know username?(y/n)”

if ($Answer = ‘n’){
    $User = Read-host -Prompt “Enter users email for lookup”
    $username = Get-ADUser -Filter "EmailAddress -eq '$User'" | select -ExpandProperty SamAccountName

    Write-Host "Username is $username, proceeding to check password" -ForegroundColor Cyan
}
else {
    $username = Read-host -Prompt “What is the username to check?”
}
$usr = Get-ADUser $username –Properties “DisplayName”, “msDS-UserPasswordExpiryTimeComputed”

$exprFileDate = $usr.“msDS-UserPasswordExpiryTimeComputed”

if ($exprFileDate -eq [int64]::MaxValue) {
# Max int64 value indicates no expiration
# This is too large to convert to datetime so add 99999 instead
    $exprDate = (Get-Date).AddDays(99999)
    $exprDays = 99999
} else {
    $exprDate = [datetime]::FromFileTime($exprFileDate)
    $exprDays = [Math]::max(0, [int]($exprDate – (Get-Date)).TotalDays)
}

[pscustomobject]@{
    Displayname = $usr.Displayname
    ExpiryDate = $exprDate
    DaysRemaining = $exprDays
}

FearsomeWhisp, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance.

To answer your question, in order to re-prompt you need to wrap the code in a loop. Take a look at the code below, untested, but you can search for samaccount or mail. If $user is NULL, then it re-prompts. The expiration date is already calculated as date rather than attempting to parse the date. Here is an example:

do {

    $search = Read-Host "Provide username or email address"

    $user = Get-AdUser -Filter {(SamAccountName -eq $search) -or (Mail -eq $search)} |
            Select DisplayName,
                   SamAccountName,
                   AccountExpirationDate,
                   @{Name='DaysRemaining';Expression={New-TimeSpan -Start $_.AccountExpirationDate -End (Get-Date) | Select -ExpandProperty Days}}

}
while (!$user)

$user

I hadn’t thought about doing it that way, its much better, thank you for the help.