2

I have a batch file that accept input from the user for the password. It does not take the last character for some reason and I cannot figure it out.

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
echo psexec \\%computerName% -u %domainName%\%userName% -p %password% cmd

I know it is not the powershell command because I tested it with set /p password="Enter password: " and still fails to echo the last character.

As per my comment, here are the if statements I used:

    set /P domainName=
    set params = %*:"=""?!
    if "%domainName%" == "1" (
      set "domainName=domain1"
      goto nextstep
    )
    if "%domainName%" == "2" (
      set "domainName=domain2"
      goto nextstep
    )
    if "%domainName%" == "3" (
      set "domainName=domain3"
      goto nextstep
    ) else (
      goto resetfile
    )

    :nextstep

My problem is that for 2 of the 3 domains, psexec runs. For domain1, it does not run. I echoed out the command, and it prints perfectly. When I remove echo from the command, it skips all of that and restarts the file.

nextstep is:

    :nextstep
    set /p userName="Enter your Admin 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
    psexec \\%computerName% -u %domainName%\%userName% -p %password% cmd
    :resetfile
    set resetfile=
    set /p resetfile="Do you want to restart this file? Press 'y' for Yes or 'n' for No then Press ENTER: "
    if not '%resetfile%'=='' set resetfile=%resetfile:~0,1%
    if '%resetfile%'=='y' goto start

The script does not even ask if I want to restart the script. It just loops back to :start, which is at the beginning of the script.

Please help, thank you!

host_255
  • 361
  • 2
  • 7
  • 18
  • I suppose the last character of the password is a special character like `&^%` or `!` and delayed expansion is enabled. Try `echo psexec.exe "\\%computerName%" -u "%domainName%\%userName%" -p "%password%" cmd`. The double quotes should be used in any case on executing `psexec.exe`. But if the password contains `%` or `!` with delayed expansion enabled, you better use `echo psexec.exe "\\%computerName%" -u "%domainName%\%userName%" -p "!password!" cmd` with of course `setlocal EnableDelayedExpansion` before. – Mofi Nov 22 '16 at 08:43
  • 4
    Honestly, couldn't you just run psexec from PowerShell as well, instead of doing an awful dance of getting a SecureString to be unsecure again? – Joey Nov 22 '16 at 10:08
  • @joey i suppose I can. – host_255 Nov 24 '16 at 04:24

1 Answers1

0

Try first this with your password, to see if it's complete

setlocal EnableDelayedExpansion
set /p password=
echo my password '!password!'

If this still fails there must be a non ascii character involved.
Then you should change the codepage with chcp.

Then you can use it with your code

psexec \\%computerName% -u %domainName%\%userName% -p !password! cmd
jeb
  • 78,592
  • 17
  • 171
  • 225
  • You don’t need `EnableDelayedExpansion` unless setting and reading the variable happens in the same block (`if`, `for`). – Melebius Nov 22 '16 at 09:30
  • 5
    @Melebius Try it without delayed expansion and my default password `^1&"&`, then you know why you want to use delayed expansion – jeb Nov 22 '16 at 10:06
  • @jeb @Melebius I actually had `setlocal EnableDelayedExpansion` in my file, but I removed it, and it was fine. Now, my problem is that for 2 of the 3 domains in the script, the file runs the psexec comamd. However, for one domain, it does not work. I may need to ask a new question. I echoed out the psexec command and it prints correctly. No spelling errors whatsoever. I'll add the if statements I used in the question. – host_255 Nov 24 '16 at 03:25
  • @Melebius I added more information about this issue in the question. Please help, thank you so much! – host_255 Nov 24 '16 at 03:30