Add convenience function (Get-Attr) for getting an attribute/member from a powershell psobject
This commit is contained in:
parent
5c61369574
commit
e4e610565c
1 changed files with 17 additions and 0 deletions
|
@ -47,6 +47,23 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 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
|
# Helper function to convert a powershell object to JSON to echo it, exiting
|
||||||
# the script
|
# the script
|
||||||
Function Exit-Json($obj)
|
Function Exit-Json($obj)
|
||||||
|
|
Loading…
Reference in a new issue