This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
forked from RamblingCookieMonster/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-PropertyType.ps1
155 lines (118 loc) · 4.75 KB
/
Get-PropertyType.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
function Get-PropertyType {
<#
.SYNOPSIS
Extract unique .NET types for properties of one or more objects
.PARAMETER InputObject
Get properties and their types for each of these
.PARAMETER Property
If specified, only return unique types for these properties
.EXAMPLE
#Define an array of objects
$array = [pscustomobject]@{
prop1 = "har"
prop2 = $(get-date)
},
[pscustomobject]@{
prop1 = "bar"
prop2 = 2
}
#Extract the property types from this array. In this example, Prop1 is always a System.String, Prop2 is a System.DateTime and System.Int32
$array | Get-PropertyType
# Name Value
# ---- -----
# prop1 {System.String}
# prop2 {System.DateTime, System.Int32}
#Pretend prop2 should always be a DateTime. Extract all objects from $array where this is not the case
$array | ?{$_.prop2 -isnot [System.DateTime]}
# prop1 prop2
# ----- -----
# bar 2
.FUNCTIONALITY
General Command
#>
param (
[Parameter( Mandatory=$true,
ValueFromPipeline=$true)]
[psobject]$InputObject,
[string[]]$property = $null
)
Begin {
#function to extract properties
Function Get-PropertyOrder {
<#
.SYNOPSIS
Gets property order for specified object
.DESCRIPTION
Gets property order for specified object
.PARAMETER InputObject
A single object to convert to an array of property value pairs.
.PARAMETER Membertype
Membertypes to include
.PARAMETER ExcludeProperty
Specific properties to exclude
.FUNCTIONALITY
PowerShell Language
#>
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromRemainingArguments=$false)]
[PSObject]$InputObject,
[validateset("AliasProperty", "CodeProperty", "Property", "NoteProperty", "ScriptProperty",
"Properties", "PropertySet", "Method", "CodeMethod", "ScriptMethod", "Methods",
"ParameterizedProperty", "MemberSet", "Event", "Dynamic", "All")]
[string[]]$MemberType = @( "NoteProperty", "Property", "ScriptProperty" ),
[string[]]$ExcludeProperty = $null
)
begin {
if($PSBoundParameters.ContainsKey('inputObject')) {
$firstObject = $InputObject[0]
}
}
process{
#we only care about one object...
$firstObject = $InputObject
}
end{
#Get properties that meet specified parameters
$firstObject.psobject.properties |
Where-Object { $memberType -contains $_.memberType } |
Select -ExpandProperty Name |
Where-Object{ -not $excludeProperty -or $excludeProperty -notcontains $_ }
}
} #Get-PropertyOrder
$Output = @{}
}
Process {
#loop through every object
foreach($obj in $InputObject){
#extract the properties in this object
$props = @( Get-PropertyOrder -InputObject $obj | Where { -not $Property -or $property -contains $_ } )
#loop through every property in this one object
foreach($prop in $props){
#try to get the property type. If it's null, say so
Try{
$type = $obj.$prop.gettype().FullName
}
Catch {
$type = $null
}
#check to see if we already have types for this prop
if(-not $Output.ContainsKey($prop)){
#we don't have an array yet. Start one, put the type in it
$List = New-Object System.Collections.ArrayList
[void]$List.Add($type)
$Output.Add($prop, $List)
}
else{
if($Output[$prop] -notcontains $type){
#type isn't in the array yet, add it
[void]$output[$prop].Add($type)
}
}
}
}
}
End {
$Output
}
}