Running powershell script from ansible playbook

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

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

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.

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.

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.

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 - 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…

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

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.

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

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"

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