I am trying to create a ps1 script that will take a users AD password as a variable then pass that to the New-Psdrive commandlet. The drive they are mapping is shared so that if they enter the correct credentials it will map, with no need to check AD for the correct username/password.
I want to be able to give them 3 chances of entering the correct credentials before the script will exit.
So far I have
$Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName")
New-PSDrive -Name "T" -PSProvider FileSystem -Root \\servername -Persist -Credential $Credential -ErrorAction Ignore
When called i get a username/password box and if credentials are correct the drive is mapped, but I want the box to pop back up if the credentials are incorrect, and potentially happen again if the wrong ones are entered again
I got as far as
try {$Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName") New-PSDrive -Name "T" -PSProvider FileSystem -Root \servername -Persist -Credential $Credential -ErrorAction Ignore} catch {write-host "incorrect, try again"}
Then repeating this, but when the credentials are correct, it still pops up the credential window
thanks for any help!