batch-filecmd

Make batch FOR /F ignore Registry AutoRun


I recently decided to up my cmd game a little and found that I could register macros and do other fun stuff using the AutoRun registry key (HKLM\Software\Microsoft\Command Processor\AutoRun).

Because I was getting tired of constantly having to look up my SSH key for git or to do start-ssh-agent I now want to run it immediately on cmd startup thus (so the plan at least) prompting me for my ssh key password once and then have it connect to the running ssh-agent on each subsequent commandline I open.

I ran into an issue however! start-ssh-agent (provided by git for windows) makes use of FOR /F in order to gather information about a potentially running ssh-agent by running tasklist /FI "Imagename eq ssh-agent.exe". From Windows Server Docs on For (emphasis mine):

You can use the for /f command to parse the output of a command by placing a back-quoted between the parentheses. It is treated as a command line, which is passed to a child Cmd.exe. The output is captured into memory and parsed as if it is a file.

That child cmd.exe is not passed /D however so in order to evaluate the tasklist command, a new cmd is opened, that runs start-ssh-agent that wants to evaluate the tasklist command so it creates a new cmd; you see where this is going.

A lot said so here goes the question: Is there any way for me to address this issue and not run into an endless creation of new commandlines by either disabling use of AutoRun for whatever is run in for /f or any other measure?


Solution

  • Following the suggestions by Stephan, aschipfl and Mofi as well as a coworker of mine, I changed approaches and switched to Cmder as my preferred terminal which has convenient setups for environments which allows running start-ssh-agent on startup as hoped.

    I moved all my doskey "aliases" to cmder\config\user_aliases.cmd and uncommented the line call "%GIT_INSTALL_ROOT%/cmd/start-ssh-agent.cmd" /k exit in cmder\config\user_profile.cmd.