runas 的别名 DOS 命令

发布于 2024-09-14 10:10:56 字数 430 浏览 2 评论 0原文

我希望能够为 dos 命令指定别名以与 runas 命令结合使用,

特别是我厌倦了获取 BIDS 的完整路径("C:\Program Files\Microsoft Visual Studio 9.0\Common7\ IDE\devenv.exe"),我想像 MS 为 ssms 所做的那样使用别名。

有人知道该怎么做吗?我知道我可以使用批处理文件来完成此任务,但我真的不想这样做。

runas /user:user /netonly bids

runas /user:user /netonly "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"

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 ("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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖兔^ω^ 2024-09-21 10:10:56

这种 doskey 技术是 SuperUser 的特色,请参阅 https://superuser.com/questions/49170/create-an-alias-in-windows-xp">https:// /superuser.com/questions/49170/create-an-alias-in-windows-xp

它的问题是您无法定义 runas 使用的别名。您可以定义一个别名,其中包含 runas 您要运行的命令,但这将无法重用。但是这样做怎么样:

SET BIDS=C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe
DOSKEY r=runas /user:user /netonly "%$1%"

然后您可以像这样使用

r bids

当然,这需要您为您想要的每个快捷方式设置一个环境变量,但我认为这并不比设置 doskey 更大的要求> 别名本身。无论如何,它不会创建文件,也不需要在 path 中放置任何内容。

更新:

我自己没有尝试过,但看起来您确实可以设置它并忘记它。

  • 可以通过 Windows 系统设置来设置环境变量(见图)
  • 每次 cmd.exe 启动时都可以设置 DOSKEY 别名 使用注册表

在此处输入图像描述

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 time cmd.exe starts using the registry

enter image description here

故事未完 2024-09-21 10:10:56

我认为您遇到的问题是命令行被评估为新用户 - 因此除非新用户也可以访问您的别名,否则它将无法工作。

编辑:您可以通过创建一个方便放置的批处理文件(或快捷方式?)来解决这个问题,该文件启动 BIDS 并运行它?

编辑:

请参阅此处此处 有关选择命令的信息

示例用法:

@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

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
一枫情书 2024-09-21 10:10:56

这是一个将在命令 shell 上设置别名的脚本。该脚本为您提供了难以捉摸的“WHERE”命令:

@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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文