bat代码中判断 bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT

·

bat 代码中判断bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT

一、判断bat是否是以管理员权限运行

@echo off
net.exe session 1>NUL 2>NUL && (
    goto as_admin
) || (
    goto not_admin
)

:as_admin
echo as_admin
goto end

:not_admin
echo not as admin

:end
pause

二、自动以管理员权限运行本CMD或BAT文件

@echo off  
net.exe session 1>NUL 2>NUL && (
    goto gotAdmin
) || (
    goto UACPrompt
)
   
:UACPrompt  
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%getadmin.vbs" 
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%getadmin.vbs" 
    "%temp%getadmin.vbs" 
    exit /B  
   
:gotAdmin  
    if exist "%temp%getadmin.vbs" ( del "%temp%getadmin.vbs" )  
 
:begin

pause

Windows下在后台运行程序

方法1:bat文件或exe程序注册成windows服务(开机后无需登录Win用户,无需进入桌面)

命令行使用sc命令.
关于sc命令的详解,请自行查看帮助(sc /?),在此只简单提及如何加入系统服务功能.
加入服务:

sc create MyServiceName binPath= 路径 start= auto

   注意:(等号后面的空格是必须的) ,启动方式自动启动auto

删除服务:

sc delete MyServiceName

例1:
将Tomcat加入到系统服务中:

sc create Tomcat binPath= "F:/apache-tomcat/bin/startup.bat" start= auto

(注意:等号和值之间应该有一个空格,  服务名称Tomcat 可以修改为您自定义的名称

将Tomcat服务删除:

sc delete Tomcat

 

例2:将"AutoStartOracle Services"加入到系统服务中:

用cmd命令提示符窗口打开程序c:a.exe,原cmd窗口关闭;

sc create MyService binPath= "cmd.exe /c start c:a.exe" start= auto displayname= "AutoStartOracle Services"
//或者
sc create MyService binPath= "C:Program Files estartOracle.bat" type= share start= auto displayname= "AutoStartOracle Services"

  

cmd /c dir 是执行完dir命令后关闭命令窗口。

cmd /k dir 是执行完dir命令后不关闭命令窗口。

cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭。

cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会关闭。

可以用cmd /?查看帮助信息。 

 例3:将Mongod.exe加入到系统服务中:  用 sc.exe 添加一个服务 以MongoDB为例      

步骤:

1.以管理员方式运行:C:WindowsSystem32cmd.exe
2.输入:sc.exe create MongoDB binPath= ""c:mongoinmongod.exe" --service --config="c:mongomongod.cfg"" DisplayName= "MongoDB" start= "auto"   //(这几个引号很重要)
3.出现:[SC] CreateService 成功
4.输入:net start MongoDB
5.出现:MongoDB 服务正在启动 .

    MongoDB 服务已经启动成功。
 
【Win】+R 然后输入 Service.msc,确定后启动服务管理器, 就能看到添加的服务了。

  

用SC命令,在注册表和服务数据库中创建服务项 的 用法:
sc <server> create [service name] [binPath= ] <option1> <option2>...

选项:
注意:   1)选项名称包括等号。  2)等号和值之间需要一个空格。
type= <own|share|interact|kernel|filesys|rec|userown|usershare>
(默认 = own)
start= <boot|system|auto|demand|disabled|delayed-auto>
(默认 = demand)
error= <normal|severe|critical|ignore>
(默认 = normal)
binPath= <.exe 文件的 BinaryPathName>
group= <LoadOrderGroup>
tag= <yes|no>
depend= <依存关系(以 / (斜杠)分隔)>
obj= <AccountName|ObjectName>
(默认= LocalSystem)
DisplayName= <显示名称>
password= <密码>

使用BAT安装 Windows Service

@echo off

@setlocal enableextensions @cd /d "%~dp0"

set InstallPath=C:DBoxServiceServer set UtilToolPath=C:WindowsMicrosoft.NETFrameworkv2.0.50727

echo Local installation folder - %InstallPath%

IF NOT EXIST "%InstallPath%" (  MKDIR "%InstallPath%"  ECHO Folder %InstallPath% created )

IF EXIST "%InstallPath%DropboxWindowsService.exe" (  %UtilToolPath%InstallUtil.exe "%InstallPath%DropboxWindowsService.exe" /u  ECHO Unregistered Service: %InstallPath%DropboxWindowsService.exe )

echo Start to copy files to service folder

copy DropboxWindowsService.exe "%InstallPath%" copy DropboxWindowsService.exe.config "%InstallPath%" copy DropboxCore.dll "%InstallPath%" copy log4net.dll "%InstallPath%"

ECHO Program files copied to %InstallPath%

%UtilToolPath%InstallUtil.exe "%InstallPath%DropboxWindowsService.exe" ECHO Registered Service (%InstallPath%DropboxWindowsService.exe)

net start DropboxWindowsService

ECHO DropBox Windows Service Installed on Server Successfully!

pause
其中出现过错误:

Uninstalling assembly 'C:DBoxServiceServerDropboxWindowsService.exe'.
Affected parameters are:
   logtoconsole = 
   assemblypath = C:DBoxServiceServerDropboxWindowsService.exe
   logfile = C:DBoxServiceServerDropboxWindowsService.InstallLog
An exception occurred while trying to find the installers in the C:DBoxServiceServerDropboxWindowsService.exe assembly.
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Aborting installation for C:DBoxServiceServerDropboxWindowsService.exe.
Installing assembly 'C:DBoxServiceServerDropboxWindowsService.exe'.

出现这个错误的原因是项目中引用的DLL名称变了,而安装包里面的DLL没有跟着改变,造成了上面的错误。

如果安装时出现“生成此程序集的运行时比当前加载的运行时新”,则可能是服务成员的.net framework 版本比脚本中调用的InstallUtil.exe高

解决办法是调用更高版本的InstallUtil.exe ,例如,黄色的为版本号
C:WINDOWSMicrosoft.NetFrameworkv4.0.30319InstallUtil.exe

方法2:使用vbs启动(开机后需要登录一个用户桌面才能执行)

 使用vbs启动,新建一个vbs脚本,内容如下:

set ws=WScript.CreateObject("WScript.Shell") 
ws.Run "test.cmd",0

·

原文地址:https://www.cnblogs.com/05-hust/p/12089839.html