Doskey doesn't recognize its own macros

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Doskey doesn't recognize its own macros

#1 Post by Quisquose » 22 Jul 2017 08:07

I created the following Doskey macro

Code: Select all

doskey su=psexec -s -i  $*

This allows me to type su <program name> to run whatever program I want with elevated privileges.

I also have various Doskey macros which I use as a shorthand way of launching programs instead of having to type out their full path. For example:

Code: Select all

doskey regwork="C:\Program Files\Registry Workshop\RegWorkshopX64.exe"

However, when I use one of these 'shorthand' macros as the program to be launched with elevated privileges, Doskey does not recognize it, and it just treats the text literally.

In other words, typing: su regwork does not work (because both su and regwork are macros).

I'd appreciate any ideas on how to make this work.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Doskey doesn't recognize its own macros

#2 Post by aGerman » 22 Jul 2017 12:14

It's not that DOSKEY doesn't recognize it because DOSKEY will not even try. Whatever you pass to macro su will be automatically passed to psexec. In your case psexec will get the string regwork passed as argument. Of course psexec doesn't know anything about the DOSKEY macro with that name. The same would happen if you write ECHO regwork in the cmd window.
You can easily work around by defining a variable instead.

Code: Select all

set regwork="C:\Program Files\Registry Workshop\RegWorkshopX64.exe"

Now use variable regwork with your macro su ...

Code: Select all

su %regwork%
... and you're done.

Steffen

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#3 Post by Quisquose » 22 Jul 2017 14:34

Thank you for the work-around and explanation, Steffen. I will use the method that you have described.

One final point.

I would just like to check whether there's any other kind of workaround that could potentially be used that would not require typing the percentage signs (or any other symbols). My preferred option would be to just type a word using ordinary characters (because it's faster, more intuitive, and that's what I already do for all my other macros). This would be better than having to remember those instances when I need to switch to wrapping the keyword in percentage signs. It just make things a little bit easier and more consistent.

I understand that the percent signs are a requirement if using variable names, but I just wanted check that I wasn't missing out on any other kind of solution that may allow me to just type regular characters.

Many thanks.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Doskey doesn't recognize its own macros

#4 Post by aGerman » 22 Jul 2017 17:21

You would still need the environment variable but you can do its expansion in the macro.

Code: Select all

doskey su=psexec -s -i  %$*%


Steffen

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#5 Post by Quisquose » 23 Jul 2017 06:14

This is exactly the kind of solution that I was hoping for. Thank you.

Unfortunately I can't get it to work though.

I am using a stripped down batch file shown below. (I removed various other macros and comments from my main batch file for purpose of testing).

Code: Select all

@ECHO Microsoft Windows [Version 6.1.7601]
:: @ECHO Copyright (c) 2009 Microsoft Corporation
@ECHO off

set regwork="C:\Program Files\Registry Workshop\RegWorkshopX64.exe"

REM  Display a simple list of directory names (with added blank line above list)
doskey ls=echo(^&dir /b

REM  run a program as 'Super User' (with Windows System-level privileges)
doskey su=psexec -s -i  %$*%

If I type %regwork% then the application launches. However when I type su regwork all I get it the psexec help commands in the console window, but the Registry Workshop application does not open.

I've tested both with and without an added: doskey regwork="C:\Program Files\Registry Workshop\RegWorkshopX64.exe" but it makes no difference.

Obviously I would like to be able to type the same regwork command whether I am using the su command or not.

Any ideas why it's not working?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Doskey doesn't recognize its own macros

#6 Post by ShadowThief » 23 Jul 2017 07:32

Quisquose wrote:If I type %regwork% then the application launches. However when I type su regwork all I get it the psexec help commands in the console window, but the Registry Workshop application does not open.


aGerman wrote:Now use variable regwork with your macro su ...

Code: Select all

su %regwork%
... and you're done

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Doskey doesn't recognize its own macros

#7 Post by aGerman » 23 Jul 2017 07:35

That's what I tried:
doskey.png
doskey.png (22.85 KiB) Viewed 12203 times

Works flawlessly for me. No idea what's going wrong with psexec. I can't test it.

Steffen

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#8 Post by Quisquose » 23 Jul 2017 11:50

Hi, the problem is something to do with this line:

doskey su=psexec -s -i %$*%


If I remove the % marks from before and after $* and then type su %regwork%, then it works fine. But if I use the command shown above (which is what you gave me to have the macro perform the expansion itself) then it doesn't work, no matter what I type.

When you say that you can't test Psexec, is it that because you don't want to, or because you don't have it? It's only 300kb so I could attach it, if it's something you'd be prepared to try.

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#9 Post by Quisquose » 23 Jul 2017 12:59

Ok, I have done some more testing and it does not seem to be psexec that is the problem. The problem is specifically with the command %$*%. I'm not sure what it's doing, but it is not enclosing the text with percentage signs (at least not on my system).

I tried using notepad as a test (to remove psexec from the equation).

I used Set command to store a path to a specific text file in a variable called %test%. Then I assigned doskey np="notepad.exe" $*.

If I type np %test%, notepad opens with the correct text file displayed (exactly as it was specified in the path using the Set command). However, if I then put percentage signs either side of the $* in the macro (so that I don't have to type them) then notepad just opens a blank document. It does not receive the path from the command %$*%.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Doskey doesn't recognize its own macros

#10 Post by aGerman » 23 Jul 2017 13:12

Quisquose wrote:When you say that you can't test Psexec, is it that because you don't want to, or because you don't have it?

Neither do I have it nor do I have any use for it. In order to do you a favour I downloaded it right now.
doskey_psexec.png
doskey_psexec.png (16.56 KiB) Viewed 12183 times

The blue window is an elevated cmd window (that I run using a shortcut to a scheduled task).
Notepad opened normally. When I closed it psexec wrote the message that notepad.exe exited with error code 0 which should be the normal behavior.

Steffen

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#11 Post by Quisquose » 23 Jul 2017 16:22

Thanks for doing that test.

As I mentioned in my last post, I had eliminated psexec as the source of the problem because I experienced exactly the same issue when I did a test using just notepad on its own. I therefore decided to try doing another test, but this time on a virtual machine, and it worked absolutely perfectly! Variables expanded without any need for me to type the % signs (which is exactly what I wanted).

So there is clearly something wrong with my main system that is messing up the expansion of %$&%. I have no idea what the problem could be, but at least I now know for sure that it's nothing to do with psexec or the way that I have been using the doskey macro commands.

Now that I know where the problem is coming from, I'll focus my attention there try to track down the culprit.

This thread is now resolved. Thanks again for all your help.

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#12 Post by Quisquose » 23 Jul 2017 17:36

Further to my post above, I have now tracked down the issue.

It wasn't any kind of system problem, but rather just something specific to the way that I was using the commands on my main system compared to what I did differently when using the virtual machine.

The purpose of me creating these macros is so that I can automatically use them whenever I open a command console. I therefore created a batch file, put all my macros in there, and then set it to run automatically when I start cmd.exe. I did this by appending the /k switch and path to the batch file containing my macros in the target box of the shortcut that I use to launch the command console. And this is where the problem lies.

When I did my test on the virtual machine it was a clean install, and so it did not have a copy of my batch file loaded by the command console. I therefore had to manually paste the commands into the console window in order to test them, whereas when I tested on my main system these commands were already listed in my startup batch file, and so I expected them to be already loaded and ready to use. This is why it worked in the virtual machine but not on my main system (even though I was using the exact same commands on both).

So I went back to my main system and manually pasted in the two commands shown below, and everything worked perfectly (for the duration of that session). But obviously once I closed and reopened the cmd console window then those manually typed commands were no longer available.

You saw the simplified batch file that I was using (I pasted it into a previous post). It contained the following two commands, and these seem to be the only two commands that don't work properly when they are loaded from a batch file.

Code: Select all

set regwork="C:\Program Files\Registry Workshop\RegWorkshopX64.exe"
doskey su=psexec -s -i  %$*%

So the problem is how to get these command to work when they are loaded a batch file without me having to physically paste them into the window for every command console session.

Presumably you were also typing the commands live into the window. Are you able to get these commands to work when they are run from a startup batch file?

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#13 Post by Quisquose » 23 Jul 2017 18:38

OK, final update.

I have managed to fix it myself (which I can't quite believe). :shock:

I was trying out various different things and I ended up running the command doskey / macros to see if there were any clues in the output. Doskey listed all of my macros exactly as I had entered them into my batch file, except for the su macro command which was displayed differently to what I had typed into my batch file. The %$*% was missing from the end of the su macro when viewed in the console window.

I therefore took a guess at how this might be fixed, and I decided to try adding another two percent signs to the %$*% command in my batch file, so that it became this:

doskey su=psexec -s -i %%$*%%


And lo and behold, it now works perfectly every time!

I appreciate your help and your patience while I worked (slowly and clumsily) through the various issues until I finally arrived at the solution.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Doskey doesn't recognize its own macros

#14 Post by aGerman » 24 Jul 2017 01:08

I think you never mentioned before that you used a batch file. There are some significant differences between a batch script and the command line. In this case variables that are not defined are considered as string in the command line but expanded to nothing in a batch script. That's the reason why you always have to double a percent sign in a batch script to get a single literal percent sign.

Steffen

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Doskey doesn't recognize its own macros

#15 Post by Quisquose » 24 Jul 2017 02:47

aGerman wrote:I think you never mentioned before that you used a batch file.Steffen

I did, actually.
Quisquose wrote:I am using a stripped down batch file shown below. (I removed various other macros and comments from my main batch file for purpose of testing).


I even posted a full copy of the batch file that I was using:
Quisquose wrote:

Code: Select all

@ECHO Microsoft Windows [Version 6.1.7601]
:: @ECHO Copyright (c) 2009 Microsoft Corporation
@ECHO off

set regwork="C:\Program Files\Registry Workshop\RegWorkshopX64.exe"

REM  Display a simple list of directory names (with added blank line above list)
doskey ls=echo(^&dir /b

REM  run a program as 'Super User' (with Windows System-level privileges)
doskey su=psexec -s -i  %$*%

But anyway, the problem is resolved now, so it's all good.

Many thanks again for your kind assistance.

Post Reply