I don't know much about scripting at all, but want to help out my lead with something. I'm trying to create a script that will ask for a username and it will return when their password will expire in AD. I also want to continue inputting usernames after it has finished in case there are multiple users I need to check. Below is the script I have so far.
$User = Read-Host "Username?"
Get-ADUser $User -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property "Displayname", @{Name = "ExpiryDate"; Expression = {[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
I was looking into doing loops, but I do not know how to write it out. Any help would be appreciated. Thanks.
CodePudding user response:
You could use a do-until
loop:
do {
$User = Read-Host "Username? (enter 'q!' to quit)"
if($User -notin @('', 'q!')){
Get-ADUser $User -Properties DisplayName,"msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property Displayname, @{Name = "ExpiryDate"; Expression = {[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
}
} until ($User -eq 'q!')
PowerShell will continue to repeat the code inside the do
block as long as the user doesn't input q!
. The if
statement also prevents attempts to fetch a user when no username was provided.
CodePudding user response:
you could also use a while loop
while($true){
$User = Read-Host "Username?"
Get-ADUser $User -Properties "DisplayName","msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "Displayname", @{Name = "ExpiryDate"; Expression = {[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
if($User -eq "q!"){
break
}
}
to quit the while loop enter "q!" in place of a user