Search code examples
variablescmdkeepass

cmd /C set variable


The short: I am trying to make a 1 line command script that sets and uses a variable. Right now I came up with:

cmd /C "set var=127.0.0.1 & echo %var%"

I would expect it to output 127.0.0.1, but instead it prints %var%.

The long: I am using keepass as a password manager and trying to make it automatically launch a RDP session for me. I put the following in the URL:

cmd://cmd /C "cmdkey /generic:TERMSRV/127.0.0.1 /user:{USERNAME} /pass:{PASSWORD} & mstsc.exe /v:127.0.0.1 & cmdkey /delete:TERMSRV/127.0.0.1"

This works, but if I need to update the ipaddress, I have to touch 3 places instead of just 1. I tried changing it to the following, but it didn't work:

cmd://cmd /C "set sServer=127.0.0.1 & cmdkey /generic:TERMSRV/%sServer% /user:{USERNAME} /pass:{PASSWORD} & mstsc.exe /v:%sServer% & cmdkey /delete:TERMSRV/%sServer%"

Solution

  • josefZ comment worked perfectly. The following command can be placed in the "URL:" field in keepass. When you double click the url or do anything else that launches the url it will automatically launch RPD and log you in.

    cmd://cmd /C "for /F "tokens=*" %G in ("127.0.0.1") do cmdkey /generic:TERMSRV/%G /user:{USERNAME} /pass:{PASSWORD} & mstsc.exe /v:%G & cmdkey /delete:TERMSRV/%G"
    

    I have tested this by RDPing into both a Windows box and a Linux box.