Hello All,
I have a PowerShell script that actually takes an argument and stops/starts a service based on the argument passed.
The script works well on the Windows machine when running locally. But fails when attempted to run via Ansible playbook.
I am posting a stack overflow link in the email which has full details of the issue.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
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": []}