使用批处理删除/安装 Windows 服务

发布于 2025-01-05 07:17:34 字数 372 浏览 0 评论 0原文

我有一个名为 serv1 的服务,我不知道它的状态(是否已安装),该服务是通过另一个批处理文件安装的(service.bat install

我的目标是编写一个 .bat 文件来检查 serv1 是否存在,因此我想删除它并重新创建 它调用“service.bat”安装';如果没有,请调用“service.bat install”创建它

我已经搜索了一些命令;我发现我们使用“ sc delete ServiceName 但是,我对批处理不太熟悉。

I've got a service called serv1 which I don't know it's state (already installed or not), this service gets installed via another batch file (service.bat install)

My goal is to write a .bat file which checks if serv1 exists, so I want to remove it and re-create it calling 'service.bat install' ; if not, create it calling 'service.bat install'

I've searched for some commands; I found that we use " sc delete ServiceName but, I'm not familiar with batch a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2025-01-12 07:17:34

希望下面的bat脚本能够帮助到你。打开记事本;复制以下代码并将其粘贴到记事本中。使用 .bat 扩展名保存文件,例如“Helper.bat”。

现在打开命令提示符,导航到放置 Helper.bat(和您的 service.bat)的目录。然后写一个这样的命令
Helper.bat serv1

这是一个通用脚本,因此您需要将相关服务名称作为参数传递。否则,只需将 %1 替换为 serv1 即可完成您的工作!

如果您需要了解有关此脚本的更多信息,请随时询问我。

这是脚本:

@echo off
sc query %1
IF ERRORLEVEL == 1060 GOTO NOT_EXIST

:Exist
    cls
    echo %1 already exist.
    SET /P result=Do you want to delete and reinstall the service? (Press Y or N)

    IF %result% == Y
            GOTO REINSTALL_SERVICE

    IF %result% == N
            GOTO End

GOTO End

:NOT_EXIST
    cls
    echo Service does not exist. Going to install now
    GOTO INSTALL_SERVICE

:REINSTALL_SERVICE
    sc delete %1
    GOTO INSTALL_SERVICE

:INSTALL_SERVICE:
    call service.bat install
    echo Installation Completed!

:End
    pause

I hope the following bat script will help you out. Open notepad; copy the following code and paste it in notepad. Save the file with .bat extension e.g. "Helper.bat".

Now open command prompt, navigate to the directory where Helper.bat (and your service.bat) are placed. Then write a command like this
Helper.bat serv1

This is a generic script so you need to pass the concerned service name as a parameter. Otherwise simply replace %1 with serv1 and get your work done!

If you need to know more about this script then feel free to ask me.

Here's the script:

@echo off
sc query %1
IF ERRORLEVEL == 1060 GOTO NOT_EXIST

:Exist
    cls
    echo %1 already exist.
    SET /P result=Do you want to delete and reinstall the service? (Press Y or N)

    IF %result% == Y
            GOTO REINSTALL_SERVICE

    IF %result% == N
            GOTO End

GOTO End

:NOT_EXIST
    cls
    echo Service does not exist. Going to install now
    GOTO INSTALL_SERVICE

:REINSTALL_SERVICE
    sc delete %1
    GOTO INSTALL_SERVICE

:INSTALL_SERVICE:
    call service.bat install
    echo Installation Completed!

:End
    pause
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文