Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the type name of a Powershell object from PSCustomObject to something I choose?

Tags:

powershell

I have a script that uses custom objects. I create them with a pseudo-constructor like this:

function New-TestResult 
{
    $trProps = @{
        name = "";
        repo = @{};

        vcs   = $Skipped;

        clean = New-StageResult; # This is another pseudo-constructor
        build = New-StageResult; # for another custom object.
        test  = New-StageResult; #     - Micah

        start = get-date;
        finish = get-date;
    }

    $testResult = New-Object PSObject -Property $trProps
    return $testResult
}

These are useful because they can be passed to something like ConvertTo-Csv or ConvertTo-Html (unlike, say, a hashtable, which would otherwise accomplish my goals). They are typed as PSCustomObject objects. This code:

$tr = new-testresult
$tr.gettype()

returns this:

IsPublic IsSerial Name                     BaseType
-------- -------- ----                     --------
True     False    PSCustomObject           System.Object

Can I change the Name field returned there from PSCustomObject to something else?

Later on when I'm collating test results, I'll pass to another function what will sometimes be an individual result, and sometimes an array of results. I need to be able to do something different depending on which of those I get.

Any help is appreciated.

like image 488
Micah R Ledbetter Avatar asked Oct 01 '12 23:10

Micah R Ledbetter


People also ask

How do you use select objects?

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.

What is PSCustomObject in PowerShell?

Long description. The [pscustomobject] type accelerator was added in PowerShell 4.0. Prior to adding this type accelerator, creating an object with member properties and values was more complicated. Originally, you had to use New-Object to create the object and Add-Member to add properties.

How do I add a property to a PowerShell object?

The Add-Member cmdlet lets you add members (properties and methods) to an instance of a PowerShell object. For instance, you can add a NoteProperty member that contains a description of the object or a ScriptMethod member that runs a script to change the object.


2 Answers

Sure, try this after creating $testResult:

$testResult.psobject.TypeNames.Insert(0, "MyType")

The heart of the PowerShell extended type system is the psobject wrapper (at least in V1 and V2). This wrapper allows you to add properties and methods, modify type names list and get at the underlying .NET object e.g.:

C:\PS > $obj = new-object psobject
C:\PS > $obj.psobject


BaseObject          :
Members             : {string ToString(), bool Equals(System.Object obj), int GetHashCode(), type GetType()}
Properties          : {}
Methods             : {string ToString(), bool Equals(System.Object obj), int GetHashCode(), type GetType()}
ImmediateBaseObject :
TypeNames           : {System.Management.Automation.PSCustomObject, System.Object}

Or try this from the prompt:

C:\PS> $d = [DateTime]::Now
C:\PS> $d.psobject
...
like image 54
Keith Hill Avatar answered Oct 09 '22 00:10

Keith Hill


I created a special cmdlet to detect the type name of the object under the powershell quickly.

For custom objects,.getType() Method cannot get the ETS type name.


function Get-PsTypeName {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true)]
        $InputObject
    )
        
    begin {

    }
        
    process {
        ((Get-Member -InputObject $InputObject)[0].TypeName)
    }
        
    end {
    }
}
like image 25
Andy Avatar answered Oct 08 '22 22:10

Andy