Tuesday, April 12, 2016

PowerShell v3 Exploring Types with NET

,
A recent project of mine has forced me to dig a lot deeper into the hidden members of a lot of objects to try and figure out why some web service methods are not working. To show how I have started piecing the puzzle together, I will retrace my steps so others can learn how to explore unknown objects with PowerShell to get a fuller understanding of what really exists beneath the hood.

To start, we will simply declare a new object,
$string = "string"
Simple enough. Now we have a string object named $string. To get the type of the object, there are two immediate approaches to use:
$string.GetType()

IsPublic IsSerial Name                                     BaseType                                                                     
-------- -------- ----                                     --------                                                                      
True     True     String                                   System.Object  

$string | Get-Member

   TypeName: System.String

Name             MemberType            Definition                                                                                       
----             ----------            ----------                                                                                       
Clone            Method                System.Object Clone()                                                                            
CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB)                                   
Contains         Method                bool Contains(string value)                                                                       
CopyTo           Method                System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)         
EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType...
Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.Str...
GetEnumerator    Method                System.CharEnumerator GetEnumerator()                                                             
GetHashCode      Method                int GetHashCode()                                                                                
GetType          Method                type GetType()                                                                                    
GetTypeCode      Method                System.TypeCode GetTypeCode()                                                                    
IndexOf          Method                int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int s...
IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char...
Insert           Method                string Insert(int startIndex, string value)                                                      
IsNormalized     Method                bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)           
LastIndexOf      Method                int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char ...
LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int LastInd...
Normalize        Method                string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)            
PadLeft          Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)                 
PadRight         Method                string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)               
Remove           Method                string Remove(int startIndex, int count), string Remove(int startIndex)                          
Replace          Method                string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)     
Split            Method                string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] ...
StartsWith       Method                bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparison...
Substring        Method                string Substring(int startIndex), string Substring(int startIndex, int length)                   
ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)                             
ToLower          Method                string ToLower(), string ToLower(System.Globalization.CultureInfo culture)                       
ToLowerInvariant Method                string ToLowerInvariant()                                                                         
ToString         Method                string ToString(), string ToString(System.IFormatProvider provider)                              
ToUpper          Method                string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)                        
ToUpperInvariant Method                string ToUpperInvariant()                                                                        
Trim             Method                string Trim(Params char[] trimChars), string Trim()                                              
TrimEnd          Method                string TrimEnd(Params char[] trimChars)                                                           
TrimStart        Method                string TrimStart(Params char[] trimChars)                                                        
Chars            ParameterizedProperty char Chars(int index) {get;}                                                                      
Length           Property              System.Int32 Length {get;}                                                                       
For such a small piece of information, these are very verbose ways to get down to brass tacks. To take it a step further, we can look at the underlying assembly with this command:
$string.GetType().Assembly

GAC    Version        Location                                                                                                           
---    -------        --------                                                                                                          
True   v4.0.30319     C:WindowsMicrosoft.NETFramework64v4.0.30319mscorlib.dll                                                       
This gives us a little info, but, we can dig deeper. What is interesting here is the fact that, if you use tab completion, these members are not accessible. Key takeaway? Sometimes you have to force PowerShell to tell you what else is out there for a given object. For example, the simply .Assembly property returns a very different set of members if you pipe it to | Select *,
$string.GetType().Assembly | select *

CodeBase            : file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
FullName            : mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
EntryPoint          :
Evidence            : {<System.Security.Policy.GacInstalled version="1"/>
                      , <System.Security.Policy.Hash version="2">
                      <hash algorithm="SHA1"
                      value="629DA705B1D63F713B6237815E8AAE169209BD36"/>
                      <hash algorithm="SHA256"
                      value="7F5383D5CA77A8BADF685D5464E2367D76207A2445587A4B1424E171561EAEBC"/>
                      <hash algorithm="MD5"
                      value="6ECA29D69B4E2DDBFC1E824A8F0BFD1A"/>
                      </System.Security.Policy.Hash>
                      , <StrongName version="1"
                      Key="00000000000000000400000000000000"
                      Name="mscorlib"
                      Version="4.0.0.0"/>

0 comments to “PowerShell v3 Exploring Types with NET”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger