How to create a service on a windows 10 machine?

I am trying to create a service of type kernel on a remote windows 10 machine from a linux ansible controller machine. To create a service I’m using sc create type=kernel binpath=c:\path command.

Ansible playbook for creating a service:

  • hosts: windows
    tasks:
  • name: Create temp service
    raw: sc create temp type=kernel binpath=c:\path\

And I’m getting the following error:

TASK [Create temp service on windows] ****************************************
fatal: [10.x.x.x]: FAILED! => {“changed”: false, “failed”: true, “rc”: 1, “stderr”: “#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="[http://schemas.microsoft.com/powershell/2004/04\](http://schemas.microsoft.com/powershell/2004/04/)”><Obj S="progress" RefId="0"><TN RefId="0">System.Management.Automation.PSCustomObjectSystem.Object<I64 N="SourceId">1<PR N="Record">Preparing modules for first use.0-1-1Completed-1 <S S="Error">Set-Content : A positional parameter cannot be found that accepts argument ‘type=kernel’.x000D__x000A<S S="Error">At line:1 char:1_x000D__x000A_<S S="Error">+ sc create temp type=kernel binpath=c:\windows\ …x000D__x000A<S S="Error">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~x000D__x000A<S S="Error"> + CategoryInfo : InvalidArgument: (:slight_smile: [Set-Content], ParameterBindingException_x000D__x000A_<S S="Error"> + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand_x000D__x000A_<S S="Error"> x000D__x000A", “stdout”: “”, “stdout_lines”: }

What could be the exact reason? Or is there any other of creating a service on a windows machine?

sc is a powershell alias for ‘set-content’

If you change your raw command to

raw: sc.exe …

it will probably work.

Better still install nssm (which is a service manager) and use win_nssm module.

There is also a win_service_configure module if you prefer for creating modules but it hasn’t been merged into ansible yet. Its here - https://github.com/ansible/ansible-modules-extras/pull/1115
perhaps you could test it?

Hope this helps,

Jon