I'd like to be able to alias a dos command to use in conjunction with the runas command
specifically I'm tired of getting the full path to BIDS (开发者_StackOverflow"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"
), and I'd like to alias like MS has done for ssms
.
Anyone know how to do this? I know I can accomplish this with a batch file, but I'd really rather not.
runas /user:user /netonly bids
vs.
runas /user:user /netonly "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"
This doskey
technique is featured over at SuperUser, see https://superuser.com/questions/49170/create-an-alias-in-windows-xp.
The problem with it is that you can't define an alias to be used by runas
. You could define an alias that included both runas
and the command you want to run, but this would not then be reusable. But what about doing this:
SET BIDS=C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe
DOSKEY r=runas /user:user /netonly "%$1%"
Which you can then use like
r bids
Of course this requires you to set an environment var for each shortcut you 'd like to have, but I think this is no greater requirement than setting the doskey
alias itself. In any case, it doesn't create a file and it doesn't require that anything be placed in the path
.
Update:
I didn't try it myself, but it certainly looks like you can set it and forget it.
- The environment variables can be set through Windows' system settings (see image)
- The
DOSKEY
alias can be set each timecmd.exe
starts using the registry
I think the problem you're hitting is that the command line is evaluated as the new user - so unless the new user also has access to your alias, it won't work.
Edit: It's possible you could work around this by creating a conveniently placed batch file (or shortcut?) which launches BIDS and RunAs'ing that?
Edit:
See here and here for info on the choice command
Sample usage:
@ECHO OFF
Echo 1. Some Command
Echo 2. Some Other Command
CHOICE /C:12 /N /T:1,10 Choose an option
IF ERRORLEVEL 2 GOTO COMMAND2
IF ERRORLEVEL 1 GOTO COMMAND1
GOTO END
:COMMAND1
Runas /Uer:Blah "BLAH" > NUL
GOTO END
:COMMAND2
Runas /Uer:Blah "BLAH" > NUL
GOTO END
:END
Here is a script that will setup alias's on your command shells. This script gives you the elusive "WHERE" command :
@ECHO OFF
ECHO Loading additional commands from:
ECHO %0
ECHO Type 'DOSKEY /MACROS:ALL' to see the configured commands.
:: to install, place this .bat script in the location you want
:: it to reside and then run this batch script with the argument "register"
IF "%1"=="register" (
REG.exe ADD "HKCU\Software\Microsoft\Command Processor\Autorun" /ve /t REG_SZ /d "%0" /f
ECHO The DOS profile is registered. Load a new command prompt and test a command.
)
@DOSKEY LS=DIR /w
@DOSKEY CP=COPY $*
@DOSKEY MV=MOVE $*
@DOSKEY H=DOSKEY /HISTORY
@DOSKEY WHERE=@for %%e in (%PATHEXT%) do @for %%i in ($*%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i
精彩评论