0

I have two files one is batch file named validator.bat and another one is powershell script checker.ps1

validator.bat

    @echo OFF
    set/p "pass=>"
    echo %pass%

The variable pass is to be assigned the value using powerhell script.

checker.ps1

    $pwd1 = Read-Host "Enter your passowrd: "
    $pwd2 = 'password'
    if ($pwd1 -ceq $pwd2) {
    Write-Host "matched"
    } else {
    Write-Host "differ"
    }

I want to assign the output string "matched"/"differ" to the pass variable in bat file but using only powershell script or its code. I searched a lot on Google and Youtube but I could not Find the solution to my problem. Please help me.

5
  • environmental variables are per session. so, if you make/change an EnvVar in cmd.exe using set ...it will only be seen by that session and sessions that are children of that session. can you do this all in one session? Commented Oct 5, 2020 at 21:06
  • Yes I can do it in one session. I would need to run full code of powershell by writing it in bat file but that won't allow me to input data. How will I get the value then? Commented Oct 5, 2020 at 21:11
  • 1
    if you do everything in powershell ... then you won't need to play with the problems of mixing BAT/CMD with PoSh. if you cannot do that, then the save to a file idea may be your only hope. Commented Oct 5, 2020 at 23:54
  • I wanted to do everything in powershell but I know the codes for bat file for some tasks which I don't know how to convert for powershell. And there is some functionality I know in powershell which I don't know how to use in Bat.😞 Commented Oct 6, 2020 at 5:47
  • thank you for the "why" of it! [grin] you will likely be better off if you work on translating things to ONE powershell script ... but if you are in an urgent hurry, then do something like what is shown in the Answer by DougMaurer. Commented Oct 6, 2020 at 16:34

1 Answer 1

2

validator.bat

@echo OFF
for /f %%a in ('powershell -file checker.ps1') do set "pass=%%~a"
echo %pass%

checker.ps1

$pwd1 = Read-Host "Enter your password: "
$pwd2 = 'password'
if ($pwd1 -ceq $pwd2) {
    Write-Host "matched"
} else {
    Write-Host "differ"
}

Note, this will not mask the password as it's typed. You'd need to use -AsSecureString on read-host and then convert it to a regular string to compare it. There may be a way to compare secure strings that I am not aware of.

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

4 Comments

You can use ConvertFrom-SecureString to convert a secure-string into a 460-character normal string. That could be stored in the script (in place of 'password') and converted to a secure-string with ConvertTo-SecureString. However, according to this answer you have to go through a slightly tortuous route to compare them.
Yeah, I had already done that part of getting secure password, hashing it and comparing it.
Thank you so much for solving my problem 🤗
Here is the answer on how to compare secure strings. stackoverflow.com/questions/38901752/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.