help returning a simple result…

Welcome Forums General PowerShell Q&A help returning a simple result…

Viewing 2 reply threads
  • Author
    Posts
    • #253547
      Participant
      Topics: 2
      Replies: 1
      Points: 16
      Rank: Member

      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
      }

    • #253553
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      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.

      • #253583
        Participant
        Topics: 2
        Replies: 1
        Points: 16
        Rank: Member

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

    • #253556
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      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:

Viewing 2 reply threads
  • You must be logged in to reply to this topic.