Tuesday, October 25, 2016

PowerShell v3 Function Get ObjectInformation

,
After working with a lot of .NET type issues lately I wanted to encapsulate the functionality I have worked on recently into a function...and, so, here we go. The great thing about this function is that it returns A LOT (yes, I mean A LOT) of information about the objects passed to the function. I put tips on how to work with it in the comment-based help, but, be sure to play around with it a bit and try using sort and select to get more meaningful output on large collections. Also, try piping output to Select -First 1 * a few times to see what properties are exposed. Feel free to provide suggestions on how to improve this one. I am sure there are lots of things that can be added, its just a matter of knowing what to put in and not overload folks.
function Get-ObjectInformation
{
      <#
     
            .SYNOPSIS
                  A function used to explore object type information.
                 
            .DESCRIPTION
                  The Get-ObjectInformation function provides access to otherwise
                  buried .NET reflection functionality. There is a large collection of
                  switch parameters to help give a large sample of the members for
                  an object passed to the function.
                 
            .NOTES
                  Author: Will Steele (wlsteele@gmail.com)
                  Last edited date: 7/19/2012
                  Version 1.0
                 
                  Some of these will return large collections, depending on the assembly
                  being referenced, so, it is sometimes a good idea to write to a variable
                  first, examine the object size (using .Count) and select -first 5. Once
                  you have a small sample set you can then determine which fields you need
                  for the specific investigation you are using. The key to realize here
                  is that many of these are in depth .NET objects and they have very
                  large property collections.
                 
            .PARAMETER Object
                  A mandatory parameter indicating the object to examine.
                 
            .PARAMETER GetConstructors
              A switch parameter indicating whether it should return the constructor
              information or not. Default is False.

            .PARAMETER GetCustomAttributes
              A switch parameter indicating whether it should return the Custom
                  Attributes or not. Default is False.

            .PARAMETER GetCustomAttributesData
              A switch parameter indicating whether it should return the
                  Custom Attributes Data or not. Default is False.

            .PARAMETER GetExportedTypes
              A switch parameter indicating whether it should return the Exported
                  Types or not. Default is False. NOTE: These are exported types of
                  the assembly as a whole, not just for the referenced type.

            .PARAMETER GetGenericArguments
              A switch parameter indicating whether it should return the Generic
                  Arguments or not. Default is False.

            .PARAMETER GetHashCode
              A switch parameter indicating whether it should return the Hash
                  Code or not. Default is False.

            .PARAMETER GetLoadedModules
              A switch parameter indicating whether it should return the Loaded
                  Modules or not. Default is False.

            .PARAMETER GetManifestResourceNames
              A switch parameter indicating whether it should return the
                  Manifest Resource Names or not. Default is False.

            .PARAMETER GetMembers
              A switch parameter indicating whether it should return the Members
              or not. Default is False.

            .PARAMETER GetMethods
              A switch parameter indicating whether it should return the Methods
              or not. Default is False.

            .PARAMETER GetModules
              A switch parameter indicating whether it should return the Modules
              or not. Default is False.

            .PARAMETER GetName
              A switch parameter indicating whether it should return the Name
              or not. Default is False.

            .PARAMETER GetReferencedAssemblies
              A switch parameter indicating whether it should return the Referenced
                  Assemblies or not. Default is False.

            .PARAMETER GetType
              A switch parameter indicating whether it should return the Type
              information or not. Default is False.

            .PARAMETER Summary
              A switch parameter indicating whether it should return the function
                  summary of which switches are $true. Default is False.     
                 
            .INPUTS
                  Object
                 
            .OUTPUTS
                  Returns a variety of output pertaining to the input objects type
                  data.
                       
            .EXAMPLE 1
                  $string = "string"
                  Get-ObjectInformation -Object $string -GetType
                  System.String
                 
            .EXAMPLE 2
                  $psobject = New-Object -TypeName PSObject
                  Get-ObjectInformation -Object $psobject -GetExportedTypes | sort name
                 
                  IsPublic IsSerial Name                                     BaseType
                  -------- -------- ----                                     --------
                  True     True     ActionPreference                         System.Enum
                  True     True     ActionPreferenceStopException            System.Management.Automation.RuntimeException
                  True     False    AdapterCodeMethods                       System.Object
                  .
                  .
                  .
                  True     False    WSManConfigurationOption                 System.Management.Automation.PSTransportOption
                  True     False    WSManConnectionInfo                      System.Management.Automation.Runspaces.RunspaceConnection...
                  True     False    WSManServerChannelEvents                 System.Object
           
            .EXAMPLE 3
                  $contentservice = New-WebServiceProxy -Uri http://services.msdn.microsoft.com/ContentServices/ContentService.asmx -Namespace contentservice
                  Get-ObjectInformation -Object $contentservice -GetReferencedAssemblies -Summary

                  Version        Name
                  -------        ----
                  4.0.0.0        mscorlib
                  4.0.0.0        System.Web.Services
                  4.0.0.0        System
                  4.0.0.0        System.Xml
                 
                  --- Information Summary ---
                 

0 comments to “PowerShell v3 Function Get ObjectInformation”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger