·

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

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

@echo off
net.exe session >NUL >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 >NUL >NUL && (
goto gotAdmin
) || (
goto UACPrompt
) :UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", >> "%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\restartOracle.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:\Windows\System32\cmd.exe
2.输入:sc.exe create MongoDB binPath= "\"c:mongo\bin\mongod.exe\" --service --config=\"c:mongo\mongod.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:\DBoxService\Server set UtilToolPath=C:\Windows\Microsoft.NET\Framework\v2.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:\DBoxService\Server\DropboxWindowsService.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\DBoxService\Server\DropboxWindowsService.exe
logfile = C:\DBoxService\Server\DropboxWindowsService.InstallLog
An exception occurred while trying to find the installers in the C:\DBoxService\Server\DropboxWindowsService.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:\DBoxService\Server\DropboxWindowsService.exe.
Installing assembly 'C:\DBoxService\Server\DropboxWindowsService.exe'.

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

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

解决办法是调用更高版本的InstallUtil.exe ,例如,黄色的为版本号
C:\WINDOWS\Microsoft.Net\Framework\v4.0.30319\InstallUtil.exe

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

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

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

·

bat代码中判断 bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT的更多相关文章

  1. python代码中判断版本

    在python代码中判断python版本: if sys.version_info < (3, 0): lib.make_flows.argtypes = [c_char_p, c_char_p ...

  2. JavaScript中判断变量类型最简洁的实现方法以及自动类型转换(#################################)

    这篇文章主要介绍了JavaScript中判断整字类型最简洁的实现方法,本文给出多个判断整数的方法,最后总结出一个最短.最简洁的实现方法,需要的朋友可以参考下 我们知道JavaScript提供了type ...

  3. 如何在 .NET 库的代码中判断当前程序运行在 Debug 下还是 Release 下

    我们经常会使用条件编译符 #if DEBUG 在 Debug 下执行某些特殊代码.但是一旦我们把代码打包成 dll,然后发布给其他小伙伴使用的时候,这样的判断就失效了,因为发布的库是 Release ...

  4. java中如何在代码中判断时间是否过了10秒

    long previous = 0L; ... { Calendar c = Calendar.getInstance(); long now = c.getTimeInMillis(); //获取当 ...

  5. android 代码中使用textAppearance

    一开始在代码中我以为使用tvAge.setTextAppearance(context, resid);这样的的方式就能行, 运行之后发现这个设置并未生效,于是到处搜索在代码中设置系统样式的的解决方法 ...

  6. 在c++代码中执行bat文件 【转】

    我想在c++代码中执行磁盘上的一个bat文件. 这个bat文件的完整路径是:E:\\7z\\my7z.bat. 方法一: system("E:\\7z\\my7z.bat"); s ...

  7. BAT批处理中的字符串处理详解(字符串截取)

    BAT批处理中的字符串处理详解(字符串截取)   BAT批处理中的字符串处理详解(字符串截取 批处理有着具有非常强大的字符串处理能力,其功能绝不低于C语言里面的字符串函数集.批处理中可实现的字符串处理 ...

  8. 利用certutil.exe实现在批处理(bat)中嵌入可执行文件或者各种媒体、图片之类二进制文件的简单方法!

    实际上利用certutil.exe 把二进制文件(包括各种文件,exe可执行程序,图片,声音,mp3) 经过base64编码为文本,可以实现把这些文件嵌入到批处理代码中. 有什么用?: 举个例子,批处 ...

  9. Windows的bat脚本中for循环

    转载至 http://123304258.blog.163.com/blog/static/12354702012621103256608/   [删除目录下某种格式的文件 ] for /r  f:\ ...

随机推荐

  1. 菜鸟linux

    //查看系统中文件的使用情况 df -h //查看当前目录下各个文件及目录占用空间大小 du -sh *//查看当期端口使用情况netstat -tlpn //find命令详见--https://ww ...

  2. mysql-主从备份问题小结

    一:防火墙 常用命令 firewall-cmd --state 或 systemctl status firewalld # 查看状态 systemctl start firewalld# 启动 sy ...

  3. Photoshop Elements2020强势来袭,教你三秒钟拯救闭眼照

    Photoshop Elements2020强势来袭,一系列的黑科技让设计师和路人都惊叹不已!若某人的闭眼成为一张集体照的败笔,那该如何挽回? 想要挽救闭眼照?听起来很高大上,很困难?不,Photos ...

  4. Jacoco收集单元测试、集成测试和系统功能测试覆盖率

    Jacoco收集单元测试.集成测试和系统功能测试覆盖率 2020-02-27  目录 1 安装版本2 被测系统代码示例3 收集单元测试覆盖率4 收集集成和功能测试覆盖率 代码覆盖率可在单元测试.系统测 ...

  5. Keras高层API之Metrics

    在tf.keras中,metrics其实就是起到了一个测量表的作用,即测量损失或者模型精度的变化.metrics的使用分为以下四步: step1:Build a meter acc_meter = m ...

  6. jQuery笔记(三)jQuery中的事件

    ; padding:0;} body { font-size:13px; line-height:130%; padding:60px;} #panel { width:300px; border:1 ...

  7. ECMAScript基本对象——Date日期对象

    1.创建 var 对象名 = new Date(); 2.方法 ①toLocaleString()据本地时间格式,把 Date 对象转换为字符串.和电脑的语言位置有关 ②getTime()返回 197 ...

  8. thinkphp5.0将数据指模板中

    在控制器中写: public function index(){ $username = \think\Db::table('navigation')->where(array("Vi ...

  9. nginx模块之ngx_http_fastcgi_module

    nginx支持LNMP 安装php-fpm yum install php-fpm -y 使用/etc/php-fpm.d/www.conf配置文件默认配置即可 打开php配置: vim /etc/n ...

  10. Spark学习之路 (十八)SparkSQL简单使用[转]

    SparkSQL的进化之路 1.0以前: Shark 1.1.x开始: SparkSQL(只是测试性的) SQL 1.3.x: SparkSQL(正式版本)+Dataframe 1.5.x: Spar ...