Thank you very much for your response and for explaining the issue in detail.
I have tried this and it worked as expected. In the playbook which you provided below, I would like to define the “cdu” as a variable since I have to identify it dynamically. (Actually, this playbook will be called by a bash script which will pass an extra argument to the playbook) I tried to do that by modifying the playbook bit.
gather_facts: no
vars:
SITE: cdu
tasks:
- name: restart website service
win_shell: |
$site = Get-Website -Name “‘{{ SITE }}’” | Where-Object { $_.Bindings.Collection.bindingInformation -like ‘:80:’ }
$serviceName = “ColdFusion 9 - {0}” -f $site.Name.Split(’ ')[0]
Stop-Service -Name $serviceName
#Start-Service -Name $serviceName
Also, tried to use the wildcard expression within the curly braces by escaping them but I ended up in the error.
I am sending the error for your reference.
`
FAILED! => {“changed”: true, “cmd”: “$site = Get-Website -Name "‘cdu’" | Where-Object { $_.Bindings.Collection.bindingInformation -like ‘:80:’ }\n$serviceName = "ColdFusion 9 - {0}" -f $site.Name.Split(’ ')[0]\n\nStop-Service -Name $serviceName\n#Start-Service -Name $serviceName”, “delta”: “0:00:00.763000”, “end”: “2020-07-29 11:31:36.584755”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2020-07-29 11:31:35.821755”, “stderr”: “You cannot call a method on a null-valued expression.\r\nAt line:2 char:1\r\n+ $serviceName = "ColdFusion 9 - {0}" -f $site.Name.Split(’ ')[0]\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : InvalidOperation: ( , RuntimeException\r\n + FullyQualifiedErrorId : InvokeMethodOnNull\r\n \r\nStop-Service : Cannot bind argument to parameter ‘Name’ because it is null.\r\nAt line:4 char:20\r\n+ Stop-Service -Name $serviceName\r\n+ ~~~~~~~~~~~~\r\n + CategoryInfo : InvalidData: ( [Stop-Service], ParameterBindin \r\n gValidationException\r\n + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M \r\n icrosoft.PowerShell.Commands.StopServiceCommand”, “stderr_lines”: [“You cannot call a method on a null-valued expression.”, “At line:2 char:1”, “+ $serviceName = "ColdFusion 9 - {0}" -f $site.Name.Split(’ ')[0]”, “+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”, " + CategoryInfo : InvalidOperation: ( , RuntimeException", " + FullyQualifiedErrorId : InvokeMethodOnNull", " ", “Stop-Service : Cannot bind argument to parameter ‘Name’ because it is null.”, “At line:4 char:20”, “+ Stop-Service -Name $serviceName”, “+ ~~~~~~~~~~~~”, " + CategoryInfo : InvalidData: ( [Stop-Service], ParameterBindin “, " gValidationException”, " + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M “, " icrosoft.PowerShell.Commands.StopServiceCommand”], “stdout”: “”, “stdout_lines”: }
`
Thank you again for your your help on this.
Regards,
Darshan