Pass module failure in windows powershell module

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

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.

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

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