Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows services 使用NSSM API检查给定服务名称是否存在及其状态_Windows Services_Windows Server 2012 R2_Nssm - Fatal编程技术网

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

Windows services 使用NSSM API检查给定服务名称是否存在及其状态,windows-services,windows-server-2012-r2,nssm,Windows Services,Windows Server 2012 R2,Nssm,我正在尝试构建一种自包含的系统,在该系统中,我将应用程序的可执行文件复制到某个位置,并将服务作为独立的应用程序运行,而无需安装。我正在使用NSSM可执行文件在windows server 2012 R2中创建服务,在一台机器上,将有许多可部署的功能。 我的问题是,在使用Ansible自动化部署时,我需要知道给定的服务名称是否已经存在,如果已经存在,它的状态是什么?NSSM中似乎没有任何API来检查这一点。 如何通过命令行询问NSSM是否存在服务? 我可以通过命令行(无powershell)检查服

我正在尝试构建一种自包含的系统,在该系统中,我将应用程序的可执行文件复制到某个位置,并将服务作为独立的应用程序运行,而无需安装。我正在使用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) 使用获取服务 Powershell命令“获取服务”可以像sc util一样为您提供有关服务的详细信息:

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

    - debug: msg="{{result}}"
3) 赢家服务模块(推荐) Ansible的模块win_服务可以通过不指定任何操作来简单地获取服务详细信息。唯一的问题是当服务不存在时,它会使任务失败。当出现错误或忽略错误时,可以使用失败的\u来应对

---
- 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

忽略错误:是
保存了我的一天!因此,现在我可以停止服务,如果它们存在的话(例如安装新的JRE),然后以“启动”状态安装它们。谢谢