Add Exit-Json and Fail-Json powershell helper functions

This commit is contained in:
Matt Martz 2014-06-18 11:48:50 -05:00
parent 74c43c94cf
commit 97f4f56286
5 changed files with 21 additions and 4 deletions

View file

@ -47,3 +47,20 @@ Function Set-Attr($obj, $name, $value)
$obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value
}
# Helper function to convert a powershell object to JSON to echo it, exiting
# the script
Function Exit-Json($obj)
{
echo $obj | ConvertTo-Json
Exit
}
# Helper function to add the "msg" property and "failed" property, convert the
# powershell object to JSON and echo it, exiting the script
Function Fail-Json($obj, $message)
{
Set-Attr $obj "msg" $message
Set-Attr $obj "failed" $true
echo $obj | ConvertTo-Json
Exit
}

View file

@ -34,4 +34,4 @@ Set-Attr $result.ansible_facts "ansible_os_family" "Windows"
Set-Attr $result.ansible_facts "ansible_distribution" $osversion.VersionString
Set-Attr $result.ansible_facts "ansible_distribution_version" $osversion.Version.ToString()
echo $result | ConvertTo-Json;
Exit-Json $result;

View file

@ -44,4 +44,4 @@ $result = New-Object psobject @{
encoding = "base64"
};
Set-Attr $result "content" $content;
echo $result | ConvertTo-Json;
Exit-Json $result;

View file

@ -29,4 +29,4 @@ $result = New-Object psobject @{
changed = $false
ping = $data
};
echo $result | ConvertTo-Json;
Exit-Json $result;

View file

@ -61,4 +61,4 @@ If ($get_md5 -and $result.stat.exists -and -not $result.stat.isdir)
Set-Attr $result.stat "md5" $path_md5;
}
echo $result | ConvertTo-Json;
Exit-Json $result;