Is this the correct way to have a PsObject as a Property of a parent PsObject?
This seems somewhat cumbersome to be the correct way and I can't dot source (see pictures at the bottom) the child PsObject's properties either.
$function1.Params | fl
InformationAction : @{InformationAction=IA1; Name=InformationAction; ParameterType=System.String; ParameterSets={[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}; IsDynamic=False; Aliases=ia; Attributes=; SwitchParameter=False}
WarningAction : @{WarningAction=IA1; Name=WarningAction; ParameterType=System.String; ParameterSets={[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}; IsDynamic=False; Aliases=ia; Attributes=; SwitchParameter=False}
ErrorAction : @{ErrorAction=IA1; Name=ErrorAction; ParameterType=System.String; ParameterSets={[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}; IsDynamic=False; Aliases=ia; Attributes=; SwitchParameter=False}
InformationVariable : @{InformationVariable=IA1; Name=InformationVariable; ParameterType=System.String; ParameterSets={[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}; IsDynamic=False; Aliases=ia; Attributes=; SwitchParameter=False}
WarningVariable : @{WarningVariable=IA1; Name=WarningVariable; ParameterType=System.String; ParameterSets={[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}; IsDynamic=False; Aliases=ia; Attributes=; SwitchParameter=False}
ErrorVariable : @{ErrorVariable=IA1; Name=ErrorVariable; ParameterType=System.String; ParameterSets={[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}; IsDynamic=False; Aliases=ia; Attributes=; SwitchParameter=False}
Here is a code snippet to duplicate what I am doing.
#Region Step 1 (create object)
$function1 = New-Object PsObject;
$function1 | Add-Member -NotePropertyName "Name" -NotePropertyValue "Test1";
$function1 | Add-Member -NotePropertyName "Params" -NotePropertyValue $null;
$param1 = New-Object PsObject;
$param1 | Add-Member -NotePropertyName "InformationAction" -NotePropertyValue "IA1";
$param1 | Add-Member -NotePropertyName "Name" -NotePropertyValue "InformationAction";
$param1 | Add-Member -NotePropertyName "ParameterType" -NotePropertyValue "System.String";
$param1 | Add-Member -NotePropertyName "ParameterSets" -NotePropertyValue "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}";
$param1 | Add-Member -NotePropertyName "IsDynamic" -NotePropertyValue "False";
$param1 | Add-Member -NotePropertyName "Aliases" -NotePropertyValue "ia";
$param1 | Add-Member -NotePropertyName "Attributes" -NotePropertyValue $null;
$param1 | Add-Member -NotePropertyName "SwitchParameter" -NotePropertyValue "False";
$param2 = New-Object PsObject;
$param2 | Add-Member -NotePropertyName "WarningAction" -NotePropertyValue "IA1";
$param2 | Add-Member -NotePropertyName "Name" -NotePropertyValue "WarningAction";
$param2 | Add-Member -NotePropertyName "ParameterType" -NotePropertyValue "System.String";
$param2 | Add-Member -NotePropertyName "ParameterSets" -NotePropertyValue "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}";
$param2 | Add-Member -NotePropertyName "IsDynamic" -NotePropertyValue "False";
$param2 | Add-Member -NotePropertyName "Aliases" -NotePropertyValue "ia";
$param2 | Add-Member -NotePropertyName "Attributes" -NotePropertyValue $null;
$param2 | Add-Member -NotePropertyName "SwitchParameter" -NotePropertyValue "False";
$param3 = New-Object PsObject;
$param3 | Add-Member -NotePropertyName "ErrorAction" -NotePropertyValue "IA1";
$param3 | Add-Member -NotePropertyName "Name" -NotePropertyValue "ErrorAction";
$param3 | Add-Member -NotePropertyName "ParameterType" -NotePropertyValue "System.String";
$param3 | Add-Member -NotePropertyName "ParameterSets" -NotePropertyValue "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}";
$param3 | Add-Member -NotePropertyName "IsDynamic" -NotePropertyValue "False";
$param3 | Add-Member -NotePropertyName "Aliases" -NotePropertyValue "ia";
$param3 | Add-Member -NotePropertyName "Attributes" -NotePropertyValue $null;
$param3 | Add-Member -NotePropertyName "SwitchParameter" -NotePropertyValue "False";
$psObjectTemp = New-Object PsObject;
$psObjectTemp | Add-Member -NotePropertyName $param1.Name -NotePropertyValue $param1;
$psObjectTemp | Add-Member -NotePropertyName $param2.Name -NotePropertyValue $param2;
$psObjectTemp | Add-Member -NotePropertyName $param3.Name -NotePropertyValue $param3;
$function1.Params = $psObjectTemp
#EndRegion / Step 1 (create object)
#Region Step 2 (update object)
$param4 = New-Object PsObject;
$param4 | Add-Member -NotePropertyName "InformationVariable" -NotePropertyValue "IA1";
$param4 | Add-Member -NotePropertyName "Name" -NotePropertyValue "InformationVariable";
$param4 | Add-Member -NotePropertyName "ParameterType" -NotePropertyValue "System.String";
$param4 | Add-Member -NotePropertyName "ParameterSets" -NotePropertyValue "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}";
$param4 | Add-Member -NotePropertyName "IsDynamic" -NotePropertyValue "False";
$param4 | Add-Member -NotePropertyName "Aliases" -NotePropertyValue "ia";
$param4 | Add-Member -NotePropertyName "Attributes" -NotePropertyValue $null;
$param4 | Add-Member -NotePropertyName "SwitchParameter" -NotePropertyValue "False";
$param5 = New-Object PsObject;
$param5 | Add-Member -NotePropertyName "WarningVariable" -NotePropertyValue "IA1";
$param5 | Add-Member -NotePropertyName "Name" -NotePropertyValue "WarningVariable";
$param5 | Add-Member -NotePropertyName "ParameterType" -NotePropertyValue "System.String";
$param5 | Add-Member -NotePropertyName "ParameterSets" -NotePropertyValue "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}";
$param5 | Add-Member -NotePropertyName "IsDynamic" -NotePropertyValue "False";
$param5 | Add-Member -NotePropertyName "Aliases" -NotePropertyValue "ia";
$param5 | Add-Member -NotePropertyName "Attributes" -NotePropertyValue $null;
$param5 | Add-Member -NotePropertyName "SwitchParameter" -NotePropertyValue "False";
$param6 = New-Object PsObject;
$param6 | Add-Member -NotePropertyName "ErrorVariable" -NotePropertyValue "IA1";
$param6 | Add-Member -NotePropertyName "Name" -NotePropertyValue "ErrorVariable";
$param6 | Add-Member -NotePropertyName "ParameterType" -NotePropertyValue "System.String";
$param6 | Add-Member -NotePropertyName "ParameterSets" -NotePropertyValue "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}";
$param6 | Add-Member -NotePropertyName "IsDynamic" -NotePropertyValue "False";
$param6 | Add-Member -NotePropertyName "Aliases" -NotePropertyValue "ia";
$param6 | Add-Member -NotePropertyName "Attributes" -NotePropertyValue $null;
$param6 | Add-Member -NotePropertyName "SwitchParameter" -NotePropertyValue "False";
$psObjectTemp = New-Object PsObject;
$psObjectTemp = $function1.Params;
$psObjectTemp | Add-Member -NotePropertyName $param4.Name -NotePropertyValue $param4;
$psObjectTemp | Add-Member -NotePropertyName $param5.Name -NotePropertyValue $param5;
$psObjectTemp | Add-Member -NotePropertyName $param6.Name -NotePropertyValue $param6;
$function1.Params = $psObjectTemp;
#EndRegion / Step 2 (update object)
After using the accepted answer, I am happy to see this
You can greatly simplify the code using [PSCustomObject]
literals and Add-Member -NotePropertyMembers
to add multiple properties stored in a hashtable
to an existing object. Also the temporary variable $psObjectTemp
isn't really necessary.
#Region Step 1 (create object)
$function1 = [PSCustomObject]@{
Name = "Test1"
Params = [PSCustomObject]@{
InformationAction = [PSCustomObject]@{
InformationAction = "IA1"
Name = "InformationAction"
ParameterType = "System.String"
ParameterSets = "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}"
IsDynamic = "False"
Aliases = "ia"
Attributes = $null
SwitchParameter = "False"
}
WarningAction = [PSCustomObject]@{
<# TODO #>
}
ErrorAction = [PSCustomObject]@{
<# TODO #>
}
}
}
#EndRegion / Step 1 (create object)
#Region Step 2 (update object)
$function1.Params | Add-Member -NotePropertyMembers @{
InformationVariable = [PSCustomObject]@{
InformationVariable = "IA1"
Name = "InformationVariable"
ParameterType = "System.String"
ParameterSets = "{[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}"
IsDynamic = "False"
Aliases = "ia"
Attributes = $null
SwitchParameter = "False"
}
WarningVariable = [PSCustomObject]@{
<# TODO #>
}
ErrorVariable = [PSCustomObject]@{
<# TODO #>
}
}
#EndRegion / Step 2 (update object)