Functions/Get-Type.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 160 161 162 163 164 165 166 167 168 169 170 |
function Get-Type { <# .SYNOPSIS Get exported types in the current session .DESCRIPTION Get exported types in the current session .PARAMETER Module Filter on Module. Accepts wildcard .PARAMETER Assembly Filter on Assembly. Accepts wildcard .PARAMETER FullName Filter on FullName. Accepts wildcard .PARAMETER Namespace Filter on Namespace. Accepts wildcard .PARAMETER BaseType Filter on BaseType. Accepts wildcard .PARAMETER IsEnum Switch to just return IsEnum types. .EXAMPLE Get-Type | Select-Object -First 1 # Default view IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False Registry System.Object .EXAMPLE #List the full name of all Enums in the current session $List = Get-Type -IsEnum | Select -ExpandProperty FullName | Sort -Unique .EXAMPLE Get-Type -Namespace System.Xaml.Hosting # Default view IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False XamlBuildProvider System.Web.Compilation.BuildProvider .EXAMPLE # Here is a list of all the properties that are returned Get-Type | Select-Object -First 1 | Get-Member -MemberType Property TypeName: System.RuntimeType Name MemberType Definition ---- ---------- ---------- Assembly Property System.Reflection.Assembly Assembly {get;} AssemblyQualifiedName Property string AssemblyQualifiedName {get;} Attributes Property System.Reflection.TypeAttributes Attributes {get;} BaseType Property type BaseType {get;} ContainsGenericParameters Property bool ContainsGenericParameters {get;} CustomAttributes Property System.Collections.Generic.IEnumerable[System.Reflection.CustomAttributeData] CustomAttributes {get;} DeclaredConstructors Property System.Collections.Generic.IEnumerable[System.Reflection.ConstructorInfo] DeclaredConstructors {get;} DeclaredEvents Property System.Collections.Generic.IEnumerable[System.Reflection.EventInfo] DeclaredEvents {get;} DeclaredFields Property System.Collections.Generic.IEnumerable[System.Reflection.FieldInfo] DeclaredFields {get;} DeclaredMembers Property System.Collections.Generic.IEnumerable[System.Reflection.MemberInfo] DeclaredMembers {get;} DeclaredMethods Property System.Collections.Generic.IEnumerable[System.Reflection.MethodInfo] DeclaredMethods {get;} DeclaredNestedTypes Property System.Collections.Generic.IEnumerable[System.Reflection.TypeInfo] DeclaredNestedTypes {get;} DeclaredProperties Property System.Collections.Generic.IEnumerable[System.Reflection.PropertyInfo] DeclaredProperties {get;} DeclaringMethod Property System.Reflection.MethodBase DeclaringMethod {get;} DeclaringType Property type DeclaringType {get;} FullName Property string FullName {get;} GenericParameterAttributes Property System.Reflection.GenericParameterAttributes GenericParameterAttributes {get;} GenericParameterPosition Property int GenericParameterPosition {get;} GenericTypeArguments Property type[] GenericTypeArguments {get;} GenericTypeParameters Property type[] GenericTypeParameters {get;} GUID Property guid GUID {get;} HasElementType Property bool HasElementType {get;} ImplementedInterfaces Property System.Collections.Generic.IEnumerable[type] ImplementedInterfaces {get;} IsAbstract Property bool IsAbstract {get;} IsAnsiClass Property bool IsAnsiClass {get;} IsArray Property bool IsArray {get;} IsAutoClass Property bool IsAutoClass {get;} IsAutoLayout Property bool IsAutoLayout {get;} IsByRef Property bool IsByRef {get;} IsClass Property bool IsClass {get;} IsCOMObject Property bool IsCOMObject {get;} IsConstructedGenericType Property bool IsConstructedGenericType {get;} IsContextful Property bool IsContextful {get;} IsEnum Property bool IsEnum {get;} IsExplicitLayout Property bool IsExplicitLayout {get;} IsGenericParameter Property bool IsGenericParameter {get;} IsGenericType Property bool IsGenericType {get;} IsGenericTypeDefinition Property bool IsGenericTypeDefinition {get;} IsImport Property bool IsImport {get;} IsInterface Property bool IsInterface {get;} IsLayoutSequential Property bool IsLayoutSequential {get;} IsMarshalByRef Property bool IsMarshalByRef {get;} IsNested Property bool IsNested {get;} IsNestedAssembly Property bool IsNestedAssembly {get;} IsNestedFamANDAssem Property bool IsNestedFamANDAssem {get;} IsNestedFamily Property bool IsNestedFamily {get;} IsNestedFamORAssem Property bool IsNestedFamORAssem {get;} IsNestedPrivate Property bool IsNestedPrivate {get;} IsNestedPublic Property bool IsNestedPublic {get;} IsNotPublic Property bool IsNotPublic {get;} IsPointer Property bool IsPointer {get;} IsPrimitive Property bool IsPrimitive {get;} IsPublic Property bool IsPublic {get;} IsSealed Property bool IsSealed {get;} IsSecurityCritical Property bool IsSecurityCritical {get;} IsSecuritySafeCritical Property bool IsSecuritySafeCritical {get;} IsSecurityTransparent Property bool IsSecurityTransparent {get;} IsSerializable Property bool IsSerializable {get;} IsSpecialName Property bool IsSpecialName {get;} IsUnicodeClass Property bool IsUnicodeClass {get;} IsValueType Property bool IsValueType {get;} IsVisible Property bool IsVisible {get;} MemberType Property System.Reflection.MemberTypes MemberType {get;} MetadataToken Property int MetadataToken {get;} Module Property System.Reflection.Module Module {get;} Name Property string Name {get;} Namespace Property string Namespace {get;} ReflectedType Property type ReflectedType {get;} StructLayoutAttribute Property System.Runtime.InteropServices.StructLayoutAttribute StructLayoutAttribute {get;} TypeHandle Property System.RuntimeTypeHandle TypeHandle {get;} TypeInitializer Property System.Reflection.ConstructorInfo TypeInitializer {get;} UnderlyingSystemType Property type UnderlyingSystemType {get;} .NOTES # inspired by https://gallery.technet.microsoft.com/scriptcenter/Get-Type-Get-exported-fee19cf7 # changed what is output by the function #> # todo Change += to System.Collections.Arraylist [CmdletBinding(ConfirmImpact = 'None')] param( [string] $Module = '*', [string] $Assembly = '*', [string] $FullName = '*', [string] $Namespace = '*', [string] $BaseType = '*', [switch] $IsEnum ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { #Build up the Where statement $WhereArray = @('$_.IsPublic') if ($Module -ne '*') { $WhereArray += '$_.Module -like $Module' } if ($Assembly -ne '*') { $WhereArray += '$_.Assembly -like $Assembly' } if ($FullName -ne '*') { $WhereArray += '$_.FullName -like $FullName' } if ($Namespace -ne '*') { $WhereArray += '$_.Namespace -like $Namespace' } if ($BaseType -ne '*') { $WhereArray += '$_.BaseType -like $BaseType' } #This clause is only evoked if IsEnum is passed in if ($PSBoundParameters.ContainsKey('IsEnum')) { $WhereArray += '$_.IsENum -like $IsENum' } #Give verbose output, convert where string to scriptblock $WhereString = $WhereArray -Join ' -and ' $WhereBlock = [scriptblock]::Create( $WhereString ) Write-Verbose -Message "Where ScriptBlock: { $WhereString }" #Invoke the search! [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { Write-Verbose -Message "Getting types from $($_.FullName)" try { $_.GetExportedTypes() } catch { Write-Verbose -Message "$($_.FullName) error getting Exported Types: $_" $null } } | Where-Object -FilterScript $WhereBlock | Select-Object -Property Name, FullName, IsPublic, IsSerializable, IsEnum, BaseType, Module, Assembly, NameSpace } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |