bat代码中判断 bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT
·
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的更多相关文章
- python代码中判断版本
在python代码中判断python版本: if sys.version_info < (3, 0): lib.make_flows.argtypes = [c_char_p, c_char_p ...
- JavaScript中判断变量类型最简洁的实现方法以及自动类型转换(#################################)
这篇文章主要介绍了JavaScript中判断整字类型最简洁的实现方法,本文给出多个判断整数的方法,最后总结出一个最短.最简洁的实现方法,需要的朋友可以参考下 我们知道JavaScript提供了type ...
- 如何在 .NET 库的代码中判断当前程序运行在 Debug 下还是 Release 下
我们经常会使用条件编译符 #if DEBUG 在 Debug 下执行某些特殊代码.但是一旦我们把代码打包成 dll,然后发布给其他小伙伴使用的时候,这样的判断就失效了,因为发布的库是 Release ...
- java中如何在代码中判断时间是否过了10秒
long previous = 0L; ... { Calendar c = Calendar.getInstance(); long now = c.getTimeInMillis(); //获取当 ...
- android 代码中使用textAppearance
一开始在代码中我以为使用tvAge.setTextAppearance(context, resid);这样的的方式就能行, 运行之后发现这个设置并未生效,于是到处搜索在代码中设置系统样式的的解决方法 ...
- 在c++代码中执行bat文件 【转】
我想在c++代码中执行磁盘上的一个bat文件. 这个bat文件的完整路径是:E:\\7z\\my7z.bat. 方法一: system("E:\\7z\\my7z.bat"); s ...
- BAT批处理中的字符串处理详解(字符串截取)
BAT批处理中的字符串处理详解(字符串截取) BAT批处理中的字符串处理详解(字符串截取 批处理有着具有非常强大的字符串处理能力,其功能绝不低于C语言里面的字符串函数集.批处理中可实现的字符串处理 ...
- 利用certutil.exe实现在批处理(bat)中嵌入可执行文件或者各种媒体、图片之类二进制文件的简单方法!
实际上利用certutil.exe 把二进制文件(包括各种文件,exe可执行程序,图片,声音,mp3) 经过base64编码为文本,可以实现把这些文件嵌入到批处理代码中. 有什么用?: 举个例子,批处 ...
- Windows的bat脚本中for循环
转载至 http://123304258.blog.163.com/blog/static/12354702012621103256608/ [删除目录下某种格式的文件 ] for /r f:\ ...
随机推荐
- 在NBA我需要翻译 适配器模式
17.1 在NBA我需要翻译! 17.2 适配器模式 Adapter,将一个类的接口转换成客户希望的另外一个接口,Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作, 有些国家 ...
- JAVA JDBC 连接数据库
方式一 Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost:3306/test ...
- Java遍历字符串数组的几种方法
1. for循环 for(int i = 0; i < fields[].length; i++){ } 2 for each循环 for(String x:fields){ } 3. JDK8 ...
- 更改 Solution (.Sln) file
Microsoft Visual Studio 2010 的项目为件改为Microsoft Visual Studio 2015默认打开 2010 的Solution (.Sln) file Micr ...
- TP框架上传图片至阿里云oss
首先安装阿里云oss扩展: composer require aliyuncs/oss-sdk-php 如果这个安装不上可以直接下载SDK的包: 链接:https://pan.baidu.com/s/ ...
- contos7命令行访问网址
1.curl访问: 例子: curl http://www.baidu.com 注意:这种访问只会直接读取网站HTML代码出来 2.elinks访问: yum -y install elinks : ...
- PHP0004:PHP基础3
php写在 标签里
- ES6扩展
模板字符串和标签模板 const getCourseList = function() { // ajax return { status: true, msg: '获取成功', data: [{ i ...
- Spring Event事件驱动
Spring事件驱动模型,简单来说类似于Message-Queue消息队列中的Pub/Sub发布/订阅模式,也类似于Java设计模式中的观察者模式. 自定义事件 Spring的事件接口位于org.sp ...
- 1级搭建类101-Oracle 11g 单实例 FS LVM(11.2.0.4+RHEL 5)公开
项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...