windows-services – 使用NSSM API检查给定的服务名称是否存在及其状态

前端之家收集整理的这篇文章主要介绍了windows-services – 使用NSSM API检查给定的服务名称是否存在及其状态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试构建一种自包含系统,我将应用程序可执行文件复制到一个位置,并将服务作为独立应用程序运行,无需安装.我使用NSSM可执行文件Windows Server 2012 R2和一台机器上创建服务,将会有很多可部署的.
我的问题是,在使用Ansible自动部署时,我陷入了需要知道某个服务名称是否已经存在的地步,如果是,那么它的状态是什么?在NSSM中似乎没有任何API来检查它.
如果服务存在,如何通过命令行询问NSSM?
我可以通过命令行检查服务的存在和状态(没有powershell)吗?

解决方法

好吧,没有办法只通过NSSM获取服务细节,所以我想出了一些其他方法获取ansible的Windows服务细节:

1)使用sc.exe命令util
   sc实用程序可以查询Windows机器以获取有关给定服务名称的详细信息.我们可以在变量中注册查询的结果,并在条件中的其他任务中使用它.

---
- hosts: windows
  tasks:
    - name: Check if the service exists
      raw: cmd /c sc query serviceName
      register: result

    - debug: msg="{{result}}"

2)使用Get-Service
   Powershell命令’Get-Service’可以像sc util一样为您提供有关服务的详细信息:

---
- hosts: windows
  tasks:
    - name: Check if the service exists
      raw: Get-Service serviceName -ErrorAction SilentlyContinue
      register: result

    - debug: msg="{{result}}"

3)win_service模块(推荐)
   Ansible的模块win_service可用于通过不指定任何操作来简单地获取服务详细信息.唯一的问题是当服务不存在而导致任务失败的情况.可以使用Failed_when或ignore_errors来解决这个问题.

---
- hosts: windows
  tasks:
     - name: check services
      win_service:
          name: serviceName
      register: result
      Failed_when: result is not defined
      #ignore_errors: yes

    - debug: msg="{{result}}"

    - debug: msg="running"
      when: result.state is not defined or result.name is not defined

猜你在找的Windows相关文章