Can't change TypeNames on ComObject?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Lido Paglia Lido Paglia 1 year, 11 months ago.

  • Author
    Posts
  • #21436
    Profile photo of Lido Paglia
    Lido Paglia
    Participant

    I'm working with COM objects and would like to insert a custom typename so that I can format the resulting object. Calling the clear or insert methods (see example below) on $track.pstypenames seems to have no effect while doing the same on $track.psobject.typenames does produce the expected outcome. However, despite being able to modify the typename collection via $track.psobject.typenames my format ps1xml file seems to have no effect. I suspect this is the case since running Get-Member on the modified track object still shows System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566} as the type name. It seems that $track.pstypenames is not read-only:

    $track.pstypenames.psbase
    
    Count          : 4
    IsReadOnly     : False
    IsFixedSize    : False
    SyncRoot       : System.Object
    IsSynchronized : False
    

    Can someone explain what's happening or point me in the right direction towards learning more about how to work with types and formatting? Can this type name manipulation only be done to custom objects and not com objects? Thanks in advance.

    Example transcript (if this doesn't render correctly here I've also made it available @ ):

    [1] scripts:\> $itunes = New-Object -ComObject itunes.application
    [2] scripts:\> $itunes.CurrentTrack
    [3] scripts:\> $itunes.play[]
    [4] scripts:\> $itunes.CurrentTrack
    
    
    Name               : [Untitled]
    Index              : 104
    sourceID           : 72
    playlistID         : 21271
    trackID            : 21377
    TrackDatabaseID    : 6032
    Kind               : 1
    Playlist           : System.__ComObject
    Album              : SAP
    Artist             : Alice in Chains
    BitRate            : 192
    BPM                : 0
    Comment            :
    Compilation        : False
    Composer           : J. Cantrell
    DateAdded          : 11/4/2014 4:02:13 PM
    DiscCount          : 0
    DiscNumber         : 0
    Duration           : 224
    Enabled            : True
    EQ                 :
    Finish             : 224
    Genre              : Rock
    Grouping           :
    KindAsString       : MPEG audio file
    ModificationDate   : 11/15/2014 9:18:55 AM
    PlayedCount        : 0
    PlayedDate         : 12/30/1899 12:00:00 AM
    PlayOrderIndex     : 4464
    Rating             : 0
    SampleRate         : 48000
    Size               : 5396996
    Start              : 0
    Time               : 3:44
    TrackCount         : 5
    TrackNumber        : 5
    VolumeAdjustment   : 0
    Year               : 0
    Artwork            : System.__ComObject
    Location           : C:\Users\Lido\Music\iTunes\iTunes Media\Music\Alice in Chains\SAP5 [Untitled].mp3
    Podcast            : False
    RememberBookmark   : False
    ExcludeFromShuffle : False
    Lyrics             :
    Category           :
    Description        :
    LongDescription    :
    BookmarkTime       : 0
    VideoKind          : 0
    SkippedCount       : 0
    SkippedDate        : 12/30/1899 12:00:00 AM
    PartOfGaplessAlbum : False
    AlbumArtist        : Alice in Chains
    Show               :
    SeasonNumber       : 0
    EpisodeID          :
    EpisodeNumber      : 0
    Size64High         : 0
    Size64Low          : 5396996
    Unplayed           : False
    SortAlbum          :
    SortAlbumArtist    :
    SortArtist         :
    SortComposer       :
    SortName           :
    SortShow           :
    AlbumRating        : 0
    AlbumRatingKind    : 1
    ratingKind         : 1
    Playlists          : System.__ComObject
    ReleaseDate        : 12/30/1899 12:00:00 AM
    
    
    
    [5] scripts:\> $track = $itunes.CurrentTrack
    [6] scripts:\> $track.pstypenames
    System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566}
    System.__ComObject
    System.MarshalByRefObject
    System.Object
    [7] scripts:\> $track.psobject.typenames
    System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566}
    System.__ComObject
    System.MarshalByRefObject
    System.Object
    [8] scripts:\> $track.pstypenames.clear[]
    [9] scripts:\> $track.pstypenames
    System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566}
    System.__ComObject
    System.MarshalByRefObject
    System.Object
    [10] scripts:\> $track.psobject.typenames.clear[]
    [11] scripts:\> $track.psobject.typenames
    [12] scripts:\> $track.pstypenames.Insert[0,'my.custom.type']
    [13] scripts:\> $track.pstypenames
    System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566}
    System.__ComObject
    System.MarshalByRefObject
    System.Object
    [14] scripts:\> $track.psobject.typenames
    [15] scripts:\> $track.psobject.typenames.Add['my.custom.type']
    [16] scripts:\> $track.psobject.typenames
    my.custom.type
    [17] scripts:\> $track.pstypenames
    System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566}
    System.__ComObject
    System.MarshalByRefObject
    System.Object
    [18] scripts:\>
    
  • #21437
    Profile photo of Don Jones
    Don Jones
    Keymaster

    For COM, I don't think interop really supports adding custom type names. it's an old tech. That means any formatting you want to do, you'll probably have to do by using a cmdlet, not an XML view. I don't know if an ETS extension would help or not.

  • #21438
    Profile photo of Lido Paglia
    Lido Paglia
    Participant

    Don, thanks so much for the quick reply.

    My workaround example for the time being...

    
    [58] scripts:\> $track = $itunes.CurrentTrack
    [59] scripts:\> $track = $track | select *
    [60] scripts:\> $track.pstypenames.Insert(0,'iTunesPS.TrackInfo')
    [61] scripts:\> $track.pstypenames
    iTunesPS.TrackInfo
    Selected.System.__ComObject
    System.Management.Automation.PSCustomObject
    System.Object
    [62] scripts:\> $track
    
    Name              Artist                  Album             TrackNumber       Time                    Rating
    ----              ------                  -----             -----------       ----                    ------
    Plugged In (Ba... Bassnectar              Divergent Spec... 2                 4:35                    0
    
    

You must be logged in to reply to this topic.