functions/Get-PSTypeExtension.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Function Get-PSTypeExtension {
    [cmdletbinding()]
    [outputtype("PSTypeExtension")]
    Param(
        [Parameter(
            Position = 0,
            Mandatory,
            HelpMessage = "Enter the name of type like System.IO.FileInfo",
            ValueFromPipelineByPropertyName,
            ValueFromPipeline
        )]
        [ValidateNotNullorEmpty()]
        [string]$TypeName,
        [Parameter(
            HelpMessage = "Enter a comma separated list of member names",
            ParameterSetName = "members"
        )]
        [string[]]$Members,
        [Parameter(HelpMessage = "Show CodeProperty custom properties")]
        [switch]$CodeProperty

    )

    Begin {
        Write-Verbose "Starting: $($MyInvocation.Mycommand)"
        $typedata = @()

    } #begin
    Process {

        Write-Verbose "Converting typename to proper type"
        $TypeName = _convertTypeName $TypeName


        Write-Verbose "Analyzing $typename"
        if ($TypeName) {
            Write-Verbose "Getting type data"
            $typedata += Get-TypeData -TypeName $typename
        }
        else {
            Write-Warning "Failed to get a typename"
            #bail out
            $typedata = $False
            return
        }

    } #process
    End {

        if ($typedata) {
            $typedata = $typedata | Select-Object -Unique
            $out = [System.Collections.Generic.List[object]]::new()
            if (-Not $Members) {
                Write-Verbose "Getting all member names"
                $Members = $typedata.members.keys
            }
            foreach ($name in $Members) {
                Try {
                    Write-Verbose "Analyzing member $name"
                    $member = $typedata.members[$name]
                    $datatype = $member.gettype().name

                    Write-Verbose "Processing type $datatype"
                    Switch ($datatype) {
                        "AliasPropertyData" {
                            $def = [pscustomobject]@{
                                PSTypename = 'PSTypeExtension'
                                MemberType = "AliasProperty"
                                MemberName = $member.name
                                Value      = $member.ReferencedMemberName
                                Typename   = $TypeName
                            }
                        } #alias
                        "ScriptpropertyData" {
                            if ($member.GetScriptBlock) {
                                $code = $member.GetScriptBlock.ToString()
                            }
                            else {
                                $code = $member.SetScriptBlock.ToString()
                            }
                            $def = [pscustomobject]@{
                                PSTypename = 'PSTypeExtension'
                                MemberType = "ScriptProperty"
                                MemberName = $member.name
                                Value      = $code
                                Typename   = $TypeName
                            }
                        } #scriptproperty
                        "ScriptMethodData" {
                            $def = [pscustomobject]@{
                                PSTypename = 'PSTypeExtension'
                                MemberType = "ScriptMethod"
                                MemberName = $member.name
                                Value      = $member.script.ToString().trim()
                                Typename   = $TypeName
                            }
                        } #scriptmethod
                        "NotePropertyData" {
                            $def = [pscustomobject]@{
                                PSTypename = 'PSTypeExtension'
                                MemberType = "Noteproperty"
                                MemberName = $member.name
                                Value      = $member.Value
                                Typename   = $TypeName
                            }
                        } #noteproperty
                        "CodePropertyData" {
                            #only show these if requested with -CodeProperty
                            if ($CodeProperty) {
                                if ($member.GetCodeReference) {
                                    $code = $member.GetCodeReference.ToString()
                                }
                                else {
                                    $code = $member.SetCodeReference.ToString()
                                }
                                $def = [pscustomobject]@{
                                    PSTypename = 'PSTypeExtension'
                                    MemberType = "CodeProperty"
                                    MemberName = $member.name
                                    Value      = $code
                                    Typename   = $TypeName
                                }
                            }
                            else {
                                $def = $False
                            }
                        } #codeproperty
                        Default {
                            Write-Warning "Cannot process $datatype type for $($typedata.typename)."
                            $def = [pscustomobject]@{
                                PSTypename = 'PSTypeExtension'
                                MemberType = $datatype
                                MemberName = $member.name
                                Value      = $member.Value
                                Typename   = $TypeName
                            }
                        }
                    }
                    if ($def) {
                        $out.Add($def)
                    }

                }
                Catch {
                    Write-Warning "Could not find an extension member called $name"
                    Write-Debug $_.exception.message
                }

            } #foreach
            #write sorted results
            $out | Sort-Object -Property MemberType, Name
        }
        else {
            Write-Warning "Failed to find any type extensions for [$Typename]."
        }
        Write-Verbose "Ending: $($MyInvocation.Mycommand)"
    }

} #end Get-PSTypeExtension