1

I was wondering if its possible to check if input matches a variable

This is the code I have right now

powershell -Command "$pword = Read-Host -Prompt 'Enter Password' -AsSecureString; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
$password=[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)" | set /p password=

if "%password%" == "test" (
    echo hi
    timeout /t 2
) else (
    echo Invalid password.
    timeout /t 2
    exit
)

I want to check if the password input matches a variable.

I tried adding if not function but it doesn't work.

  • What do you mean by "doesn't work"? The code that actually attempts to compare the password is not included in your snippet. – Christian.K Jan 20 '23 at 15:41
  • Perhaps this can help you : [Login and Register system in Batch File](https://stackoverflow.com/a/44609770/3080770) – Hackoo Jan 20 '23 at 15:52

2 Answers2

0

UPDATE: the core of this question is (of course SO has seen this before) a duplicate. But I'll leave this answer because the OP had multiple other issues in his code.


There are a couple of things wrong with your code:

  • you cannot use batch line continuations (^) inside the PowerShell code
  • you must write your password to stdout from inside your PowerShell code, or it will simply not be "seen" by the surrounding batch file code
  • You cannot get the output (see above point) by using | set /P var=. If you want to this, use a the batch for syntax.

Putting this all together, you could come up with something like this:

@rem might want to put this here, to reduce output clutter.
@echo off

@rem store PowerShell code in batch variable for better handling
@rem note the Write-Host at the end of the PowerShell code to output the password.

set psargs=$pword = Read-Host -Prompt 'Enter Password' -AsSecureString;
set psargs=%psargs% $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword);
set psargs=%psargs% $password=[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR);
set psargs=%psargs% Write-Host $password

@rem call PowerShell and get the output into the batch variable "password".

for /F usebackq %%i in (`powershell -NoProfile -Command "%psargs%"`) do (
    set password=%%~i
)

@rem do your test.

if "%password%" == "test" (
    echo hi
    timeout /t 2
) else (
    echo Invalid password.
    timeout /t 2
    exit
)

Other ways to achieve the same are also possible. For example, you could also test your password inside the PowerShell code and use exit 1 or exit 0 to signal to the batch code (using if %ERRORLEVEL% ...) if the password was correct or not. Or simply write the whole script in PowerShell in the first place.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
0

You should capture with for /f do command the password passed from PowerShell to Batch :


Here is an example :

@echo off
Title Check typed password
Call :CheckPassword
::----------------------------------------------------------------------------------------------
:CheckPassword
Mode con cols=55 lines=3
cls & color 0B & echo.
set MyPassword=Test
set "psCommand=powershell -Command "$pword = read-host 'Enter your 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
if "%MyPassword%"=="%password%" (Goto:Good) else (Goto:Bad)
exit/b
::----------------------------------------------------------------------------------------------
:Good
Cls & Color 0A
echo(
echo                     Good Password
TimeOut /T 2 /NoBreak>nul
Exit
::----------------------------------------------------------------------------------------------
:Bad
Cls & Color 0C
echo(
echo                      Bad password
TimeOut /T 1 /NoBreak>nul
Goto:CheckPassword
::----------------------------------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70