·
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\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",0
·