1

I have some code inside a batch file that when I run it my passwords do get hidden. However I am trying to build it so that I can place the code inside any batch file and you will then need to enter the correct password. Currently you can type anything and be allowed access. I know my code isn't written so you have to type something specific in, that is why I am asking here.

@echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' - 
AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
    [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%

I wish to keep my code as it is because it currently hides what gets typed, but I need someone to add some sort of password checker.

piet.t
  • 11,718
  • 21
  • 43
  • 52
connorg98
  • 189
  • 12
  • 1
    Possible duplicate of [Batch File Command Hide Password](https://stackoverflow.com/questions/36291324/batch-file-command-hide-password) – tukan Jul 03 '18 at 10:30
  • @tukan No i already have mine hidden i want to make it so you have to enter the correct password using the code i have already written – connorg98 Jul 03 '18 at 10:46
  • 1
    You do the password checking against what entity? Where is the correct password? – tukan Jul 03 '18 at 10:58
  • @tukan That is the point i cant figure out how to fit in or were to fit in the password checking. "I know my code isn't written so you have to type something specific in, that is why i am asking here." – connorg98 Jul 03 '18 at 11:08
  • I don't understand the issue. Your script is accessing where? We don't know your infrastructure. You are trying to check the windows user password or against some active directory server or against some DB? What is your workflow. You need to be specific for us to understand your issue. – tukan Jul 03 '18 at 11:15
  • @tukan My script doesnt have to access anywere i am simply asking how to get my code to request a specific password i am not trying to access anything like a DB i want to be able to use my code in any type of Batch file that when i open them E.g A Batch Game that it requests a password through the CMD – connorg98 Jul 03 '18 at 11:20
  • I see. If you use https://stackoverflow.com/a/36294190/6059896 have it before your Game code you will have the requested password in your variable. You can also do a `CALL get_password.bat` (if you store the batch code and get the return value from that CALL. – tukan Jul 03 '18 at 11:37
  • @tukan Ok mate thank you ill give it a test and get back to you thanks – connorg98 Jul 03 '18 at 11:38
  • 1
    `if "%password%" neq "MySecretString" goto :eof` – Stephan Jul 03 '18 at 15:13
  • 1
    Hey @tukan your solve worked thank you very much for showing me to the correct page! – connorg98 Jul 04 '18 at 08:53
  • @Stephan Nice solve Stephan but somebody beat you to the post aha thank you anyway! – connorg98 Jul 04 '18 at 08:53
  • @connorg98 you are wlecome :). – tukan Jul 04 '18 at 09:00
  • @tukan Would you like to add your comment as an answer and i will select it as top answer? – connorg98 Jul 04 '18 at 12:58
  • @connorg98 that would be nice. Thank you. – tukan Jul 04 '18 at 12:59

1 Answers1

0

Adding the answer based on comments, so this question is answered:

If you use Batch File command hide password have it before your Game code you will have the requested password in your variable.

You can also do a CALL get_password.bat (if you store the batch code and get the return value from that CALL).

Thank you Connorg98.

tukan
  • 17,050
  • 1
  • 20
  • 48