I want to create an object in powershell that stores information about the state of a script. I can do this:
$myScriptObject =
@("status", "Selected Operation(s):", "None"),
("status", "Current Operation:", "None"),
("status", "Current Step:", "Prompting for Script Action" ),
("test", "This is just for testing", "1,2,3") `
| ForEach-Object {[pscustomobject]@{kind = $_[0]; name = $_[1]; value
= $_[2]}}
And that works:
$myScriptObject
kind name value
---- ---- -----
status Selected Operation(s): None
status Current Operation: None
status Current Step: Prompting for Script Action
test This is just for testing 1,2,3
...and I can even do this:
foreach($myObject in $myScriptObject) {
if ($myObject.kind -eq 'status') {
Write-Host $myObject.name $myObject.value
}
}
which outputs this:
Selected Operation(s): None
Current Operation: None
Current Step: Prompting for Script Action
My questions are: 1. how do I add something like the following to $myScriptObject:
-kind "ActionMenuChoice" -Name "Do This" -Value 1
-kind "ActionMenuChoice" -Name "Do That" -Value 2
How do I change items already in the object?
status Current Step: Prompting for Script Action
to
status Current Step: Prompting for Login
Or am I going about it all wrong? The idea came from the difficulty in returning numerous variables back from a function, and I read using objects is much better to pass back and forth in functions, and found using objects to be much easier to keep track of and to a certain extent manipulate.
Cheers!
$Collection[$Index].PropertyName = 'New Value'..Nameproperty, which makes updating existing objects easier.