Add examples for how to use powershell common functions
This commit is contained in:
parent
e4e610565c
commit
df8866b8bd
1 changed files with 5 additions and 0 deletions
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
# Helper function to parse Ansible JSON arguments from a file passed as
|
# Helper function to parse Ansible JSON arguments from a file passed as
|
||||||
# the single argument to the module
|
# the single argument to the module
|
||||||
|
# Example: $params = Parse-Args $args
|
||||||
Function Parse-Args($arguments)
|
Function Parse-Args($arguments)
|
||||||
{
|
{
|
||||||
$parameters = New-Object psobject;
|
$parameters = New-Object psobject;
|
||||||
|
@ -42,6 +43,7 @@ Function Parse-Args($arguments)
|
||||||
# Helper function to set an "attribute" on a psobject instance in powershell.
|
# Helper function to set an "attribute" on a psobject instance in powershell.
|
||||||
# This is a convenience to make adding Members to the object easier and
|
# This is a convenience to make adding Members to the object easier and
|
||||||
# slightly more pythonic
|
# slightly more pythonic
|
||||||
|
# Example: Set-Attr $result "changed" $true
|
||||||
Function Set-Attr($obj, $name, $value)
|
Function Set-Attr($obj, $name, $value)
|
||||||
{
|
{
|
||||||
$obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value
|
$obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value
|
||||||
|
@ -66,6 +68,7 @@ Function Get-Attr($obj, $name, $default = $null)
|
||||||
|
|
||||||
# Helper function to convert a powershell object to JSON to echo it, exiting
|
# Helper function to convert a powershell object to JSON to echo it, exiting
|
||||||
# the script
|
# the script
|
||||||
|
# Example: Exit-Json $result
|
||||||
Function Exit-Json($obj)
|
Function Exit-Json($obj)
|
||||||
{
|
{
|
||||||
echo $obj | ConvertTo-Json
|
echo $obj | ConvertTo-Json
|
||||||
|
@ -74,6 +77,7 @@ Function Exit-Json($obj)
|
||||||
|
|
||||||
# Helper function to add the "msg" property and "failed" property, convert the
|
# Helper function to add the "msg" property and "failed" property, convert the
|
||||||
# powershell object to JSON and echo it, exiting the script
|
# powershell object to JSON and echo it, exiting the script
|
||||||
|
# Example: Fail-Json $result "This is the failure message"
|
||||||
Function Fail-Json($obj, $message)
|
Function Fail-Json($obj, $message)
|
||||||
{
|
{
|
||||||
Set-Attr $obj "msg" $message
|
Set-Attr $obj "msg" $message
|
||||||
|
@ -84,6 +88,7 @@ Function Fail-Json($obj, $message)
|
||||||
|
|
||||||
# Helper filter/pipeline function to convert a value to boolean following current
|
# Helper filter/pipeline function to convert a value to boolean following current
|
||||||
# Ansible practices
|
# Ansible practices
|
||||||
|
# Example: $is_true = "true" | ConvertTo-Bool
|
||||||
Function ConvertTo-Bool
|
Function ConvertTo-Bool
|
||||||
{
|
{
|
||||||
param(
|
param(
|
||||||
|
|
Loading…
Reference in a new issue