2

Complete Powershell Noobie here.

I have a .bat file that I would like to convert to Powershell.

Basically when run; it asks the user to enter their Active Directory credentials. Once validated; it starts a RSAT tool (example: dhcpmgmt.msc) as the elevated domain user.

However, if credentials are incorrect (if %ERRORLEVEL% EQU 5); it does a GOTO an echo "Incorrect username and password" and then loops back requesting the user to enter their credentials again.

When I use:

do 
{
  Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Credential (Get-Credential "$env:USERDNSDOMAIN\")  -ArgumentList "C:\Windows\System32\dhcpmgmt.msc"
} until ($response -eq $null)

It works. But if I enter an incorrect password; the window closes.

I would like a notification information the user that the Username/Password is incorrect and then re-direct them to enter their credentials again. This would loop until either the user enters the correct credentials or simple clicks the cancel button.

Any help/guidance is highly appreciated. Thanks a bunch in advance!

1
  • You'll want to Try-Catch the credentials check. Also, you may want to allow the user to be able to store their credentials in an encrypted file. although it you're using the launch of rsat to check them then you don't likely make the try-catch function, instead try to auth to AD to verify the credentials using a try-catch and a loop for your credential portion and then launch rsat separately Commented Feb 11, 2020 at 16:23

2 Answers 2

1

You can run this in the while loop to keep asking for credentials until they are valid.

  • Take credentials
  • Run powershell with new creds
  • If fails, ask for new credentials. Break out of the loop if no.
while ($true) 
{
  $userCreds = Get-Credential "$env:USERDNSDOMAIN\"
  try {
     Start-Process powershell -Credential $userCreds -ArgumentList "C:\Windows\System32\dhcpmgmt.msc" -ErrorAction Stop
     break
  }
  catch {
     Write-Output "Invalid Credentials"
     Write-Output "Enter new credentials?"
     if ((Read-Host) -ne "Yes") { break }

  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This worked! Thank you. The only thing remaining is when I enter the credentials successfully - the program runs but the credentials windows remain open, I have to manually close it. How to have it closed automatically after program runs...?
@PerDeSi Add the return or break statement after Start-Process command. that will take care of it
That did it, Thanks!
0

This should do the needful.

  • Loop to Get and Check Credential
  • Try-Catch Credential Check
  • Raise Messagebox on Failed Credential
  • Exit script on Cancel.
$Credential=$null
while ($Credential -eq $null) {
  $Credential = Get-Credential -Username "$env:USERDNSDOMAIN\" -message "Enter Admin Password"
  try {
    Start-Process -FilePath cmd.exe /c -Credential $Credential
  }
  catch {
    $Credential=$null
    IF (
      $([System.Windows.MessageBox]::Show('Invalid UN/PW!  ?','Credential Error','OKCancel','Error')
    ) -eq "Cancel") { EXIT}
  }
}

### Your code to launch RSAT tools here:

We use a while loop.

While the credentials are NULL we p, getting the credentials, storing it as a secure string in a variable, then do a try-catch on the test of running a process, if that will try to get the credentials again, and we store the credentials in a variable as a secure string.

Initially, credentials are NULL so we ask for them.

Then we use Try to test the credentials by initiating a process as them.

If that fails we Catchit and 1st set credentials to NULL again, then we Raise a Windows Message Box to the user letting them know the credentials failed and checking if they would like to try again (OK) or Cancel.

If they respond with Cancel, there is no point in continuing the script so we just exit.

Otherwise, we just go ahead with our loop as planned

2 Comments

Thanks Ben! However, When attempting use your cmdlet, I get the following error: Unable to find type [System.Windows.MessageBox]. At [location of file.ps1]:10 char:9 + $([System.Windows.MessageBox]::Show('Invalid UN/PW! ?','Creden ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Windows.MessageBox:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound My Code: ### Your code to launch RSAT tools here: Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList "C:\Windows\System32\dhcpmgmt.msc"
What version of PowerShell are you running 2.0?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.