From e4e610565c4e6e5121e173ca68e96d705323a223 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 19 Jun 2014 11:32:32 -0500 Subject: [PATCH] Add convenience function (Get-Attr) for getting an attribute/member from a powershell psobject --- lib/ansible/module_utils/powershell.ps1 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index 86c3c82e5b1..37366f0d61f 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -47,6 +47,23 @@ Function Set-Attr($obj, $name, $value) $obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value } +# Helper function to get an "attribute" from a psobject instance in powershell. +# This is a convenience to make getting Members from an object easier and +# slightly more pythonic +# Example: $attr = Get-Attr $response "code" -default "1" +Function Get-Attr($obj, $name, $default = $null) +{ + If ($obj.$name.GetType) + { + $obj.$name + } + Else + { + $default + } + return +} + # Helper function to convert a powershell object to JSON to echo it, exiting # the script Function Exit-Json($obj)