# Pass module failure in windows powershell module

**URL:** <https://forum.ansible.com/t/pass-module-failure-in-windows-powershell-module/18434>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [June 8, 2015, 6:27pm UTC](https://forum.ansible.com/t/pass-module-failure-in-windows-powershell-module/18434 "2015-06-08T18:27:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [June 8, 2015, 6:27pm UTC](https://forum.ansible.com/t/pass-module-failure-in-windows-powershell-module/18434/1 "2015-06-08T18:27:06Z")

</div>

I’ve written a handful of windows modules and haven’t found a good way to tell ansible that the module failed.

Does anyone have an elegant way to do this? I could use failed\_when in the task but I’d rather be able to return something to tell ansible there has been a failure.

Thanks

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 8, 2015, 7:31pm UTC](https://forum.ansible.com/t/pass-module-failure-in-windows-powershell-module/18434/2 "2015-06-08T19:31:38Z")

</div>

Use the “fail-json” builtin function to report failure.

Fail-Json -obj $result -message “Could not find package $package”

Have a look at the built-in windows modules.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [June 8, 2015, 8:18pm UTC](https://forum.ansible.com/t/pass-module-failure-in-windows-powershell-module/18434/3 "2015-06-08T20:18:52Z")

</div>

Thanks not sure how I missed this, I knew there had to be a helper method.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 15, 2015, 12:45pm UTC](https://forum.ansible.com/t/pass-module-failure-in-windows-powershell-module/18434/4 "2015-06-15T12:45:13Z")

</div>

BTW, if you need to report failure because a required option/parameter/argument was not sent, you can use “failifempty” and just point to the output object. This way you don’t have to “if thru” all the options. This example will report failure if the “refresh\_mode” option is not set:

$params = Parse-Args $args;

$result = New-Object psobject;

Set-Attr $result “changed” $false;

$RefreshMode = Get-Attr -obj $params -name refresh\_mode -failifempty $true -resultobj $result
