PoshCode Logo PowerShell Code Repository

Get-Field by Andrew Savinykh 6 years ago
View followups from Andrew Savinykh | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2057"></script>download | new post

Displays private fields of passed object. This function is useful only if you want to want to see what is under the hood in powershell

  1. function Get-Field{
  2. [CmdletBinding()]
  3.         param (
  4.                 [Parameter(Position=0,Mandatory=$true)]
  5.                 $InputObject
  6.         )
  7.        
  8.         $type = $InputObject.gettype()
  9.        
  10.         $publicNonPublic = [Reflection.BindingFlags]::Public -bor [Reflection.BindingFlags]::NonPublic
  11.         $instance = $publicNonPublic -bor [Reflection.BindingFlags]::Instance
  12.         $getField = $instance -bor [Reflection.BindingFlags]::GetField
  13.        
  14.         $fields = $type.GetFields($instance)
  15.        
  16.         $result = @{}
  17.         $fields | Foreach-Object { $result[$_.Name] =  $type.InvokeMember($_.Name, $getField, $null, $InputObject, $null) }
  18.        
  19.         $result
  20.        
  21. }
  22.  
  23. ##Example:
  24. ##$context = (Get-Field $ExecutionContext)._context
  25. ##$context
  26. ##Get-Field $context
  27. ##$sessionState = (Get-Field $context)._enginesessionstate
  28. ##$sessionState
  29. ##$moduleTable = (Get-Field $sessionState)._moduleTable
  30. ##$moduleTable

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me