英美烟草运行以admin(W / O快捷键) [英] Run BAT as admin (w/o shortcut)

查看:229
本文介绍了英美烟草运行以admin(W / O快捷键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想创建一个.bat后TASKKILL在Win7的具体方案。

我使用的命令:

TASKKILL / F / IMLCore.exe

中的.bat需要为了工作它被以管理员身份运行似乎让我创建了一个快捷方式,它在管理模式自动运行在另一个线程指定(<一个href=\"http://stackoverflow.com/questions/6811372/how-to-$c$c-a-bat-file-to-always-run-as-admin-mode\">How至code BAT文件始终运行的管理模式?)。

但是,使用Microsoft鼠标和键盘中心时,我的地图附加键以运行该快捷方式,它会自动运行快捷方式的目标,而不是快捷方式本身不具备管理员权限需要(在选择时地图快捷方式,它会自动改变路径,目标,手动设置路径返回一个未找到的错误)。

所以基本上我在想,如果有不涉及创建快捷方式以提升的权限自动运行.BAT另一种方式。


解决方案

  @ECHO OFFopenFiles散&GT; NUL 2  - ;&放大器; 1
IF%ERRORLEVEL%==0GOTO:YouAreAdmin
GOTO:GetAdmin:GetAdmin
ECHO.Set UAC = ^的CreateObject(Shell.Application^)&GT; StartAsAdmin.vbs
ECHO.UAC.ShellExecute%〜FS0,,,运行方式,1&GT;&GT; StartAsAdmin.vbs
StartAsAdmin.vbs
DELStartAsAdmin.vbs
EXIT / B:好
REM你code从这里开始!
回声。
回声。如果你看到这一点,你已经开始为这个管理BAT文件。
暂停&GT; NUL
出口

很抱歉,但我是新的! :)

因此​​,首先, openFiles散命令检查,如果你拼命地跑程序作为管理员,这样需要管理员权限的任何命令。如果您键入&GT; NUL 2 - ;&放大器; 1 后命令,CMD将隐藏该命令的任何消息并重定向错误。所以,如果你有麻烦运行像 openFiles散一个简单的命令行动,你可以有唯一的错误是批处理文件doesen't具有管理员权限。如果%ERRORLEVEL%为0(你运行该命令没有错误),看来你启动的应用程序作为管理员,所以要步:确定标签。但是,如果%ERRORLEVEL%不是0(有问题),似乎应用doesen't具有管理员权限。因此它将automatly开始如从由该批处理文件生成的临时的VBScript管理员,然后开始时,然后被删除。

So, I am trying to create a .bat to taskkill a specific program in Win7.

I am using the command:

taskkill /f /im "LCore.exe"

The .bat needs to be run as admin in order to work it seems so I have created a shortcut to it to run automatically in admin mode as specified in another thread (How to code a BAT file to always run as admin mode?).

However, when using the Microsoft Mouse and Keyboard Center to map one of my additional keys to run the shortcut it automatically runs the target of the shortcut rather than the shortcut itself which doesn't have the admin privileges needed (when selecting to map the shortcut it automatically changes the path to the target, manually setting the path returns a 'not found' error).

So basically I was wondering if there is another way that doesn't involve creating a shortcut to automatically run a .bat with elevated privileges.

解决方案

@ECHO OFF

OPENFILES>NUL 2>&1
IF "%ERRORLEVEL%"=="0" GOTO :YouAreAdmin
GOTO :GetAdmin

:GetAdmin
ECHO.Set UAC = CreateObject^("Shell.Application"^) > "StartAsAdmin.vbs"
ECHO.UAC.ShellExecute "%~fs0", "", "", "runas", 1 >> "StartAsAdmin.vbs"
StartAsAdmin.vbs
DEL "StartAsAdmin.vbs"
EXIT /B

:OK
REM Your code starts here!
ECHO.
ECHO. If you see this, you have started as admin this bat file.
PAUSE>NUL
EXIT

Sorry, but I am new! :)

So first, OPENFILES command checks if you have runned program as administrator, like any command that needs administrator privileges. If you type >NUL 2>&1 after a command, CMD will hide any message from that command and will redirect error. So if you have troubles running a simple command action like OPENFILES, the only error that you can have is that batch file doesen't have administrator privileges. And if %ErrorLevel% is 0 (you have no errors running that command), it seems that you started application as administrator, so it steps to :OK label. But if %ErrorLevel% is not 0 (there is a problem), it seems that application doesen't have administrator privileges. So it will start automatly as administrator from a temporary VBScript generated by that batch file, then is started, and then deleted.

这篇关于英美烟草运行以admin(W / O快捷键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆