I'm using vb.net to remotely run some powershell commands, the output of the powershell.invoke is of the
ObjectModel.Collection(Of PSObject)
Type.
I'd like to cast these to usable vb.net class / collection. When I debug the code, and hover over the output / ObjectModel.Collection(Of PSObject) the data is shown as;
(0) {@{Name=John;Books=System.Collections.ArrayList}}
(1) {@{Name=Gerry;Books=System.Collections.ArrayList}}
How can I cast this into usable vb.net class/object?
I've try to make a custom class like so;
Class BookOwner
public name as string
public Books as systems.collections.arraylist
end class
and then cast it to the object;
dim MyBookOwner as BookOwner = trycast(Output(0).BaseObject, BookOwner)
output is of the objectmodel.collection(of PSObject) type.
But after the trycast the MyBookOwner stays empty.
How can I cast the ObjectModel.Collection(Of PSOject) to a usable vb.net object.
Based on what I can see of the PSObject
type and not having done any actual testing, I think that you may be able to do this:
Dim bookOwners As New List(Of BookOwner)
For Each pso In myPsObjectCollection
Dim props = pso.Properties
Dim name = CStr(props.Single(Function(pspi) pspi.Name = "Name").Value)
Dim books = DirectCast(props.Single(Function(pspi) pspi.Name = "Books").Value, ArrayList)
bookOwners.Add(New BookOwner With (.Name = name, .Books = books})
Next
Of course, you could also condense that into a LINQ query if you wanted to.