2

I'm trying to autoload Putty Pageant with some SSH-keys with a batch script, but because I want to bypass the error message of Pageant that it is already running, I placed it in an IF-statement. For some reason however, it doesn't work:

tasklist /FI "IMAGENAME eq pageant.exe" 2>NUL | find /I /N "pageant.exe">NUL
if %ERRORLEVEL%==1 ( :: checks whether pageant.exe is running or not

    :: set the SSH-keys
    if exist "C:\SSH keys\key1.ppk" (set KEYS="C:\SSH keys\key1.ppk")
    if exist "C:\SSH keys\key2.ppk" (set KEYS=%KEYS% "C:\SSH keys\key2.ppk")

    if not defined KEYS (
        msg * A SSH-key is propably missing.
    )

    :: Start pageant with the defined SSH-keys
    start /d"C:\Program Files (x86)\PuTTY" pageant.exe %KEYS%

)

While they do work separately: (1)

 tasklist /FI "IMAGENAME eq pageant.exe" 2>NUL | find /I /N "pageant.exe">NUL
 if %ERRORLEVEL%==1 ( :: checks whether pageant.exe is running or not

       :: This works!
       start /d"C:\Program Files (x86)\PuTTY" pageant.exe

 )

(2)

:: set the SSH-keys
if exist "C:\SSH keys\key1.ppk" (set KEYS="C:\SSH keys\key1.ppk")
if exist "C:\SSH keys\key2.ppk" (set KEYS=%KEYS% "C:\SSH keys\key2.ppk")

if not defined KEYS (
    msg * A SSH-key is propably missing.
)

This works as well!
start /d"C:\Program Files (x86)\PuTTY" pageant.exe %KEYS%

Is it a syntax problem?

2
  • A job for Stackoverflow? Commented Jan 5, 2013 at 20:50
  • 1
    @GuyThomas Stack Overflow isn't particularly happy about questions that just consist of code and a request to fix it. The OP should explain what exactly fails instead of saying that "it doesn't work", and show what they've narrowed the problem down to. Batch scripting questions are on topic here and don't need to be migrated away, especially when it comes down to scripting for software that doesn't even have to do with programming (e,g. PuTTy). Commented Jan 5, 2013 at 21:00

1 Answer 1

4

Without an error message I can only guess.

Do you have delayed expansion enabled? See setlocal /?.

Add this to the beggining of your script setlocal EnableExtensions EnableDelayedExpansion and endlocal to the end of your script.

This allows the KEYS variable to evaluate the the actual value. Delayed expansion allows the value to be set immediately to the variable not just when the scope of the if statement has ended. Also don't forget to use ! instead of % for the variables set inside the if statement.

Example: (Run this in a .bat once with the EnableDepayedExpansion and once without and you will see the difference.)

setlocal EnableExtensions EnableDelayedExpansion

set "Value=Hello World"
echo %Value%
if 1==1 (
    set "Value=Goodbye World"
    echo %Value%
    echo !Value!
)
echo %Value%

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

2 Comments

That sounds promising. I will try that tomorrow. Thanks in advance!
Fantastic! That works. It is indeed the combination between EnableDepayedExpansion and the exclamation marks that did the trick.

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.