bat脚本获取管理员权限
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
写在bat文件的顶端就可以实现
用java调用时可以使用管理员权限调用,并传递参数
ProcessBuilder processBuilder = new ProcessBuilder("cmd","/c",exportbatpath);
processBuilder.directory(new File(exportbatpath. substring(0, exportbatpath. lastIndex0f("\\"))));
String[] adminCmd = {"cmd","/c","start","/wait",erportbatpath);
Process adminProcess = Runtime.getRuntime().exec(adminCmd);
if (adminProcess.waitFor() == 0){
log.info( "mongodb备份成功");
}
隐藏黑窗口的BAT脚本
@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit
:begin
::
要执行的脚本
判断进程是否存在
tasklist|find /i "mysqld.exe"
IF ERRORLEVEL 1(
net start mysql
)