简单的方法来运行一个批处理作为管理员使用JavaScript [英] Simple method to run a batch as Administrator using javascript

查看:286
本文介绍了简单的方法来运行一个批处理作为管理员使用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得自动的简单可靠的方法提升正​​在运行的批处理,而无需使用额外的VBS文件或升高的快捷方式,在其他线程建议。从调用通过JavaScript批UAC对话框确保短期简单code。

下面自动脚本正确提升用户管理员权限,当是在对话框中选择,但错误对话框弹出窗口(CMD窗口之外)的 Windows无法找到'test.bat的的' 。它可以是由于路径到文件包含空格?如何修复code或燮preSS这个错误弹出?

 关闭@echo
::测试管理员权限
> NUL 2 - ;&放大器; 1网文件
回声'%ERRORLEVEL%
MSHTA的javascript:VAR壳=新的ActiveXObject('shell.application'); shell.ShellExecute(%〜NX0,'','','运行方式',1);关闭();
::测试管理员权限
> NUL 2 - ;&放大器; 1网文件
回声'%ERRORLEVEL%
如果!错误级别! EQU 0回声你好>的%temp%\\ tst.txt
退出/ B


解决方案

我固定的剧本,现在它运行很好。根据我的研究,这是最简单可靠的方法来动态给予的任何地方出版了运行的批处理为Cmd的会话期间普通用户管理员权限。

它不需要使用函数,混合分批放大器; VBS结构,多余的文件或升高的快捷方式。它原产于Windows。用户可以添加自己的任务,code在:用户code 部分批处理运行

 关闭@echo
SETLOCAL EnableDelayedExpansion
::测试并获得管理员权限
CD / D%〜DP0&安培;回声/
如果不是%1==UAC(
    > NUL 2 - ;&放大器; 1网文件和放​​大器;&安培;回声得到管理员权限|| (回声无管理员权限和放大器; ^
MSHTA的javascript:VAR壳=新的ActiveXObject('shell.application'); shell.ShellExecute(%〜snx0,UAC,,运行方式,1);关闭();))
::重新测试管理员权限
回声/&安培; > NUL 2 - ;&放大器; 1网文件和放​​大器;&安培; (GOT呼应管理员权限和放大器;回声/)|| (回声无管理员权限退出...&放大器;转到:完):用户code
::添加code在这里
回声执行管理任务
回声你好> C:\\ test.txt的:结束
超时/吨5个NUL
退出/ B

I want to derive a simple reliable method to auto elevate a running batch without using extra VBS files or elevated shortcuts, proposed in other threads. Calling the UAC dialog from the batch via javascript ensures the short simple code.

The script below auto elevates a user to Admin rights correctly, when Yes is chosen in the dialog, but the error dialog popups (outside of Cmd window) "Windows cannot find 'test.bat'". Can it be since the path-to-file includes spaces? How to fix the code or suppress this error popup?

@echo off
:: test admin rights
>nul 2>&1 net file
echo '%errorlevel%'
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute("%~nx0", '', '', 'runas', 1);close();"
:: test admin rights
>nul 2>&1 net file
echo '%errorlevel%'
if !errorlevel! equ 0 echo Hello >%temp%\tst.txt
exit /b

解决方案

I fixed the script, and now it runs great. As per my research, this is the simplest reliable way to dynamically give a regular user Administrator privileges for the duration of that Cmd session in a running batch published anywhere.

It doesn't require using functions, hybrid batch & VBS constructs, extra files or elevated shortcuts. It is native to Windows. Users can add their own task code in the :usercode section to run by the batch.

@echo off
setlocal EnableDelayedExpansion
:: test and acquire admin rights
cd /d %~dp0 & echo/
if not "%1"=="UAC" (
    >nul 2>&1 net file && echo Got admin rights || (echo No admin rights & ^
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute("%~snx0", 'UAC', '', 'runas', 1);close();"))
:: re-test admin rights
echo/ & >nul 2>&1 net file && (echo Got admin rights & echo/) || (echo No admin rights. Exiting... & goto :end)

:usercode
:: add your code here
echo Performing admin tasks
echo Hello >C:\test.txt

:end
timeout /t 5 >nul
exit /b

这篇关于简单的方法来运行一个批处理作为管理员使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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