mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Add convenience function (Get-Attr) for getting an attribute/member from a powershell psobject
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user