# Powershell script works locally on windows machine but fails when attempted to run via playbook.

**URL:** <https://forum.ansible.com/t/powershell-script-works-locally-on-windows-machine-but-fails-when-attempted-to-run-via-playbook/33498>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [July 27, 2020, 4:29am UTC](https://forum.ansible.com/t/powershell-script-works-locally-on-windows-machine-but-fails-when-attempted-to-run-via-playbook/33498 "2020-07-27T04:29:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darshan\_Yamakanamard](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@Darshan\_Yamakanamard](https://forum.ansible.com/u/Darshan_Yamakanamard)\
**Post date:** [July 27, 2020, 4:29am UTC](https://forum.ansible.com/t/powershell-script-works-locally-on-windows-machine-but-fails-when-attempted-to-run-via-playbook/33498/1 "2020-07-27T04:29:32Z")

</div>

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.

[https://stackoverflow.com/questions/63100158/powershell-script-runs-locally-fine-but-throws-error-when-executed-via-ansibble?noredirect=1#comment111595021\_63100158](https://stackoverflow.com/questions/63100158/powershell-script-runs-locally-fine-but-throws-error-when-executed-via-ansibble?noredirect=1#comment111595021_63100158)

Any support on this would be much appreciable.

Sorry for not including all the details on the email. I believe it is more readable on stack-overflow.

Regards,  
Darshan

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 27, 2020, 8:15am UTC](https://forum.ansible.com/t/powershell-script-works-locally-on-windows-machine-but-fails-when-attempted-to-run-via-playbook/33498/2 "2020-07-27T08:15:43Z")

</div>

So breaking down your error message you get the following PowerShell error(s)

FINDSTR: Bad command line

Stop-Service : Cannot find any service with service name 'ColdFusion 9 - '.  
At C:\Users\Administrator\Documents\servicerestart.ps1:7 char:1

- Stop-Service -Name “ColdFusion 9 - $ServiceName”
- 

```auto

```

- CategoryInfo : ObjectNotFound: (ColdFusion 9 - :String) [Stop-Service], ServiceCommandException
- FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StopServiceCommand

Start-Service : Cannot find any service with service name 'ColdFusion 9 - '.  
At C:\Users\Administrator\Documents\servicerestart.ps1:8 char:1

- Start-Service -Name “ColdFusion 9 - $ServiceName”
- 

```auto

```

- CategoryInfo : ObjectNotFound: (ColdFusion 9 - :String) [Start-Service], ServiceCommandException
- FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StartServiceCommand

The first error ‘FINDSTR: Bad command line’ is saying that the call to findstr in your script didn’t have a valid argument. My guess is that ‘findstr $SITE’ is the culprit and ‘$SITE’ isn’t actually set to anything hence it’s just calling findstr with no arguments. The remaining 2 errors are due to it trying to restart the service 'ColdFusion 9 - ’ with not service name at the end because the first command failed to find a value. My advice to you is to keep things simple and stop mixing files and technologies. You should

- Stop using findstr and focus on pure PowerShell cmdlets

- Get-Website produces an object so there’s no reason to parse text as an output making findstr or other string matching tools unneeded at all

- While it can be possible just embed the script contents in the win\_shell task instead of relying on the file being present on the remote host  
So what I would do is the following win\_shell task

- name: restart website service  
win\_shell: |  
$site = Get-Website -Name ‘_cdu_’ | Where-Object { $\_.Bindings.Collection.bindingInformation -like ‘_:80:_’ }  
$serviceName = “ColdFusion 9 - {0}” -f $site.Name.Split(’ ')[0]

Stop-Service -Name $serviceName  
Start-Service -Name $serviceName

This will get any site that matches the name ‘_cdu_’ and has a binding on port 80. I’m not sure where the findstr production aspect comes here so if you need any futher help filtering the object I would need to know more information about the environment. I also used the yaml multiline string format ‘key: |’ where the next indented lines are treated like a normal string value. This pretty much enables you to inline the PowerShell script in the task itself keeping the task closer to what it is actually doing.

---

<div class="post-metadata">

**Author:** ![Darshan\_Yamakanamard](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@Darshan\_Yamakanamard](https://forum.ansible.com/u/Darshan_Yamakanamard)\
**Post date:** [July 29, 2020, 11:44pm UTC](https://forum.ansible.com/t/powershell-script-works-locally-on-windows-machine-but-fails-when-attempted-to-run-via-playbook/33498/3 "2020-07-29T23:44:14Z")

</div>

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.

- hosts: CF-DEV-B

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

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 30, 2020, 3:38am UTC](https://forum.ansible.com/t/powershell-script-works-locally-on-windows-machine-but-fails-when-attempted-to-run-via-playbook/33498/4 "2020-07-30T03:38:03Z")

</div>

This part ‘“_‘{{ SITE }}’_”’ means it will template the raw PowerSHell script as 'Get-WebSite -Name “_‘cdu’_” (with the inner single quotes). My guess is you want this to be ‘Get-Website -Name “_{{ SITE }}_”’. There’s no need to quote a jinja2 block unless you are starting the YAML value with {.

I would test to see once you’ve fixed the Name part to see if it even found any websites. It’s best to make sure that this has returned the site you need before trying to integrate it into the script.

---

<div class="post-metadata">

**Author:** ![Darshan\_Yamakanamard](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@Darshan\_Yamakanamard](https://forum.ansible.com/u/Darshan_Yamakanamard)\
**Post date:** [July 30, 2020, 6:12am UTC](https://forum.ansible.com/t/powershell-script-works-locally-on-windows-machine-but-fails-when-attempted-to-run-via-playbook/33498/5 "2020-07-30T06:12:26Z")

</div>

Thanks a ton. It works as expected now. I appreciate your support.  
Have a great day ahead. Stay safe.

Regards,  
Darshan
