我怎么能脚本一个蝙蝠或cmd停止和启动一个服务可靠的错误检查(或让我知道它不成功的原因)?
当前回答
I have created my personal batch file for this, mine is a little different but feel free to modify as you see fit. I created this a little while ago because I was bored and wanted to make a simple way for people to be able to input ending, starting, stopping, or setting to auto. This BAT file simply requests that you input the service name and it will do the rest for you. I didn't realize that he was looking for something that stated any error, I must have misread that part. Though typically this can be done by inputting >> output.txt on the end of the line.
%var%只是让用户能够将自己的服务输入其中的一种方式,而不是每次想要启动/停止不同的服务时都必须修改bat文件。
如果我错了,任何人都可以随意纠正我。
@echo off
set /p c= Would you like to start a service [Y/N]?
if /I "%c%" EQU "Y" goto :1
if /I "%c%" EQU "N" goto :2
:1
set /p var= Service name:
:2
set /p c= Would you like to stop a service [Y/N]?
if /I "%c%" EQU "Y" goto :3
if /I "%c%" EQU "N" goto :4
:3
set /p var1= Service name:
:4
set /p c= Would you like to disable a service [Y/N]?
if /I "%c%" EQU "Y" goto :5
if /I "%c%" EQU "N" goto :6
:5
set /p var2= Service name:
:6
set /p c= Would you like to set a service to auto [Y/N]?
if /I "%c%" EQU "Y" goto :7
if /I "%c%" EQU "N" goto :10
:7
set /p var3= Service name:
:10
sc start %var%
sc stop %var1%
sc config %var2% start=disabled
sc config %var3% start=auto
其他回答
您可以使用NET START命令,然后检查ERRORLEVEL环境变量,例如。
net start [your service]
if %errorlevel% == 2 echo Could not start service.
if %errorlevel% == 0 echo Service started successfully.
echo Errorlevel: %errorlevel%
免责声明:这是我从脑子里写出来的,但我认为它会起作用。
手动重启服务是ok -服务。msc有“重新启动”按钮,但在命令行中,sc和net命令都没有“重新启动”开关,如果在cmd/bat文件中计划重新启动,服务将立即停止和启动,有时它会得到一个错误,因为服务还没有停止,它需要一段时间来关闭东西。
这可能会产生一个错误: sc停止 sc开始
插入超时是个好主意,我使用ping(它每1秒ping一次): sc停止 Ping localhost -n 60 sc开始
我正在用c#写一个windows服务,停止/卸载/构建/安装/启动循环太累人了。写了一个小脚本,命名为reploy.bat,并放在我的Visual Studio输出目录中(其中包含构建的可执行服务)来自动化循环。
只要设置这3个变量
servicename:显示在Windows服务控制面板(services.msc)
Slndir:包含解决方案(.sln)文件的文件夹(不是完整路径)
Binpath:从构建到服务可执行文件的完整路径(不是文件夹路径)
注意:需要从Visual Studio开发人员命令行运行msbuild命令才能工作。
SET servicename="My Amazing Service"
SET slndir="C:dir\that\contains\sln\file"
SET binpath="C:path\to\service.exe"
SET currdir=%cd%
call net stop %servicename%
call sc delete %servicename%
cd %slndir%
call msbuild
cd %bindir%
call sc create %servicename% binpath=%binpath%
call net start %servicename%
cd %currdir%
也许这能帮助到某些人:)
下面是Windows 10使用批量启动系统还原的命令:
sc config swprv start= Auto
你可能也会喜欢这些命令:
更改注册表值为自动启动系统恢复 REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" /v DisableSR /t REG_DWORD /d 0 /f 创建系统还原点 Wmic.exe /Namespace:\root\default Path SystemRestore Call CreateRestorePoint "djibe saved your PC", 100,12 修改“系统恢复”磁盘使用率 vssadmin调整shadowstorage /for=C: /on=C: /maxsize=10%
享受
使用SC(服务控制)命令,它为您提供了更多的选项,而不仅仅是启动和停止。
DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. USAGE: sc <server> [command] [service name] ... The option <server> has the form "\\ServerName" Further help on commands can be obtained by typing: "sc [command]" Commands: query-----------Queries the status for a service, or enumerates the status for types of services. queryex---------Queries the extended status for a service, or enumerates the status for types of services. start-----------Starts a service. pause-----------Sends a PAUSE control request to a service. interrogate-----Sends an INTERROGATE control request to a service. continue--------Sends a CONTINUE control request to a service. stop------------Sends a STOP request to a service. config----------Changes the configuration of a service (persistant). description-----Changes the description of a service. failure---------Changes the actions taken by a service upon failure. qc--------------Queries the configuration information for a service. qdescription----Queries the description for a service. qfailure--------Queries the actions taken by a service upon failure. delete----------Deletes a service (from the registry). create----------Creates a service. (adds it to the registry). control---------Sends a control to a service. sdshow----------Displays a service's security descriptor. sdset-----------Sets a service's security descriptor. GetDisplayName--Gets the DisplayName for a service. GetKeyName------Gets the ServiceKeyName for a service. EnumDepend------Enumerates Service Dependencies. The following commands don't require a service name: sc <server> <command> <option> boot------------(ok | bad) Indicates whether the last boot should be saved as the last-known-good boot configuration Lock------------Locks the Service Database QueryLock-------Queries the LockStatus for the SCManager Database EXAMPLE: sc start MyService
推荐文章
- 如何在Mac上的命令行安装JQ ?
- 在Windows批处理脚本中格式化日期和时间
- 映射一个网络驱动器供服务使用
- 如何在windows中使用命令提示符(cmd)列出文件。我试过在Linux中使用“ls”,但它显示一个错误?
- 在命令行中使用Firefox截取完整页面的截图
- 在pip install -U中“-U”选项代表什么
- 如何将参数转发到bash脚本中的其他命令?
- Windows上最好的免费c++分析器是什么?
- 如何在Windows上运行多个Python版本
- 运行计划任务的最佳方式
- Windows上Git文件的权限
- 如何同时安装Python 2。3. Python。Windows下的x
- 使用Wget进行多个同时下载?
- Unix列表命令'ls'可以输出数值chmod权限吗?
- 如何从命令行重置Jenkins安全设置?