1
@ECHO OFF     

:: Ask for Credentials
IF NOT EXIST containers/api (
    set /p login="Enter your git username: "
    powershell -Command $pword = read-host "Enter password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt
)

the code above doesn't work with the error message:

[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) was unexpected at this time.

The same code without IF, works just fine

@ECHO OFF     

:: Ask for Credentials

set /p login="Enter your git username: "
powershell -Command $pword = read-host "Enter password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt

what I'm doing wrong?

6
  • I recommend writing your script in PowerShell instead of trying to run PowerShell as a separate process because you can't ask for a password in a batch file. Much better to rewrite the script in PowerShell instead. Commented Jun 1, 2015 at 19:51
  • Bill, if I put powershell line outside of IF - that works. Commented Jun 1, 2015 at 19:54
  • This looks like a parsing problem to me. This seems like the ; in the statement is terminating the powershell command in the if case or something like that. If you quote the entire powershell command string does it work? Commented Jun 1, 2015 at 20:03
  • I had this problem before: stackoverflow.com/q/6478606/419 the unquoted ) at SecureStringToBSTR($pword) prematurely closes the IF parenthesis. You'll need to do a bit of string quoting malarky to get this to work. Don't forget you can use single ' quotes in PowerShell too. Commented Jun 1, 2015 at 20:15
  • okay - thank you guys. Once quoted - everything worked. Thanks Etan & Kev! Commented Jun 1, 2015 at 20:20

1 Answer 1

0

Okay this is how it worked

:: Ask for Credentials
IF NOT EXIST containers/webapp (
    :: Ask for Credentials
    set /p login="Enter your git username: "
    powershell -Command "$pword = read-host 'Enter password' -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)" > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt 
)

then you can use %password%. I used it to automate repo cloning from batch file. Hope this helps

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.