Win_service name option contains non permitted characters and doesn't allow escaping

Hi All

I am trying to use thw win_service module to start a service.

My issue is the service name contains non-valid characters and I dont seem to be able escape the name correctly.

The service name is this:- “SAS [config-Lev1] SASMeta - Metadata Server”.

Any regexp or normal escaping of characters fails.

task code is:

  • name: Start Metadata Server

win_service:

name: “SAS [config-Lev1] SASMeta - Metadata Server”

state: started

Any ideas or is it a bug?

Bob

Have you tried using the service name rather than the Display Name?

This is the service name. They happen to match.

Looks like an issue with the Get-Service cmdlet which in turn affects the win_service Ansible module. I’ve raised a bug for this https://github.com/ansible/ansible/issues/37621.

You can test it out by just trying to run in PowerShell manually

Get-Service -Name 'SAS [config-Lev1] SASMeta - Metadata Server'

This shouldn’t return anything as that cmdlet is interpreting the chars as a string modifier instead of a string literal.

To get the stat side of win_service working you just need to change the lines

https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/windows/win_service.ps1#L60
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/windows/win_service.ps1#L389

to

$svc = Get-Service | Where-Object { $_.Name -eq $name }

It looks like this will allow you to get the service details back but it will probably fail when trying to stop and start it as I see some other *-Service cmdlets being used with the -Name parameter. The foolproof way to get it all working is to no work with a service with those chars but that may be hard to do in your environment.

Hopefully we should have a fix in soon for this.

Thanks

Jordan