# Running powershell script from ansible playbook

**URL:** <https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 17, 2016, 2:57pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457 "2016-05-17T14:57:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [May 17, 2016, 2:57pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/1 "2016-05-17T14:57:06Z")

</div>

Control node:

- CentOS 7

- Ansible 2.1

Remote node:

- Windows 7

- Powershell 3.0

I have a powershell script (warning.ps1) on my remote node that runs fine when I double click on it. I also have a copy of the powershell script in my control node playbook directory. As I understand, the following play ought to run the powershell script

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [May 17, 2016, 3:21pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/2 "2016-05-17T15:21:27Z")

</div>

That all looks correct- I’m assuming your script has output that you’re not seeing in stdout/stderr? What should it be doing?

---

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [May 17, 2016, 3:25pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/3 "2016-05-17T15:25:45Z")

</div>

Warning.ps1 contains “Invoke-Item C:\Temp\warning.exe” warning.exe is a .dot net console program that just displays a warning message on the screen of the computer for 15 seconds and then closes.

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [May 17, 2016, 4:22pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/4 "2016-05-17T16:22:14Z")

</div>

WinRM runs in an isolated session- you won’t generally be able to do things that display stuff to an interactive user. There are ways to hack around it, but it’s not generally accepted and Microsoft makes it harder with each Windows release.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [May 17, 2016, 4:34pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/5 "2016-05-17T16:34:01Z")

</div>

Very interesting. The process shows up in task manager for exactly 15 seconds just as it should but the console is nowhere to be found. I’d like to know if anyone knows a way around this.

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [May 17, 2016, 7:02pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/6 "2016-05-17T19:02:04Z")

</div>

Yeah- it’s displaying on a headless winstation, not a user-interactive one (by Windows/WinRM design). There are various hacks to get around it, but none of them are foolproof (you can’t make assumptions about which session is interactive, or which user might see it in the case of multiple). Microsoft will tell you “don’t do it”, especially for a service you don’t own (eg, WinRM). More info at: [https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspx](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspx) - the stuff on WTSSendMessage() might be of interest to you as a workaround, though you still have to figure out which user(s)/sessions you want to notify…

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 18, 2016, 9:22am UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/7 "2016-05-18T09:22:22Z")

</div>

May be worth taking a step back and letting us know what you are trying to achieve?

If you are trying to alert users you might be able to do something like this:

ansible windowsboxes -m raw -a ‘msg “\*” /SERVER:LOCALHOST /W /TIME:2 “This is a test message, please ignore”’

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [May 18, 2016, 2:58pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/8 "2016-05-18T14:58:50Z")

</div>

Yes, that is exactly what I was looking for. Just a way to inform the user that a new software deployment is about to begin.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 18, 2016, 3:08pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/9 "2016-05-18T15:08:59Z")

</div>

Glad that helps.

We’ve discovered recently that having users logged in during deployments sometimes causes failures due to file and process locking.

If anyone has a good way of kicking off interactive users (from windows hosts) via ansible please share it.

Jon

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 18, 2016, 3:29pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/10 "2016-05-18T15:29:58Z")

</div>

Maybe you can just do a shutdown?  
"shutdown - Allows you to shut down or restart a local or remote computer. Used without parameters, shutdown will logoff the current user."

You might have to use "shutdown -lf"

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 19, 2016, 3:27pm UTC](https://forum.ansible.com/t/running-powershell-script-from-ansible-playbook/21457/11 "2016-05-19T15:27:32Z")

</div>

Thanks for this, I’ll give it a try.  
Jon
