I have a module that does a "get" and returns some information from a VMware host. All the return information is in a NoteProperty. I'd like to add in an type into that PSObject so that I can pipe it into a "set" cmdlet and inherit the property via ValueFromPipelineByPropertyName.
Trying to do something like this:
$obj = new-object PSObject
$obj | Add-Member Noteproperty -Name DataStoreName -value 'MYDATASTORE'
$obj | Add-Member ??? -Name VmHost -value $vmhost
And a get-member would look something like this:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
DataStoreName NoteProperty System.String DataStoreName=MYDATASTORE
VmHost ????? VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl
Where VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl represents the result of 'get-vmhost' from the VMware PowerCli cmdlets.
I'd like the user to be able to access $obj.DatastoreName which is just a NoteProperty or something more advanced like $obj.VmHost.ExtensionData.OverallStatus which is a property of the $vmhost object I got from Powercli.
I've been looking all around and can't figure out how to add a specific object type to an existing PSobject property.
Thanks,
Ben
Best Answer
The question around how do I get PowerShell to use a specific type is quite simple, because you don't need to do anything if you can already provide the result of a command that provides the type you want by using NoteProperty.
So in your example with
$vmHost
populated from the result ofGet-VMHost
you could do either of the following:or
If the $vmHost variable is not in the correct type already you would need to convert/cast the type or run a command that will get you the type before you add the NoteProperty.
Hope that helps.