Develmann
Develmann

Reputation: 53

Changing Type on ConvertFrom-CSV object

I am stuck on something, and I cannot figure out what the issue is. I have a command that generates CSV formatted text for output. I have successfully converted the CSV output to a Powershell Object by using ConvertFrom-CSV. Everything works great! I wanted to apply some custom formatting, so I wanted to convert the object to a custom pstype name. I cannot get it to change the type. ConvertFrom-CSV outputs a PSCustomObject, so I figured it would be easy, but so far no luck.

My Code:

##This would be a function "get-devices" that creates command text and outputs the CSV output
##This all works properly
function get-devices {
     $Command.Invoke() | ConvertFrom-Csv
}
$GAMOBJ = get-devices
$GAMOBJ.PSObject.TypeNames.Insert(0,"System.Gam.Device")
$GAMOBJ

The object prints properly, but still as a PSCustomObject, thus the formatting is not applying. What am I missing?

Any help is appreciated, Thanks in advance.

Here is the output from Get-Member:

       TypeName: Selected.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()                            
    AssetID     NoteProperty System.String AssetID=ASSETID            
    Expiration  NoteProperty System.String Expiration=2000-01-01          
    Location    NoteProperty System.String Location=LOCATION          
    Model       NoteProperty System.String Model=CHROMEBOOKMODEL
    OU          NoteProperty System.String OU=/                           
    Serial      NoteProperty System.String Serial=123456                 
    Status      NoteProperty System.String Status=ACTIVE

Someone added below that I had filtered the object with Select-Object. I removed the select object, and the member type is unchanged.

    TypeName: System.Management.Automation.PSCustomObject

Upvotes: 3

Views: 280

Answers (2)

mklement0
mklement0

Reputation: 439247

You must insert your ETS type name into each object's .PSTypeNames collection (aka .psobject.TypeNames):

$GAMOBJ.ForEach({ $_.PSTypeNames.Insert(0, "System.Gam.Device") })

Otherwise, you're only applying the type name to the array as a whole, which isn't relevant for printing the objects one by one.

Upvotes: 3

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

You've piped $GAMOBJ to Select-Object at some point after setting your custom type name (notice the type name displayed by Get-Member is Selected.System.Management.Automation.PSCustomObject)

Select-Object creates a new object for you, which is why the custom type name you've inserted doesn't persists.

Upvotes: 1

Related Questions