PoshCode Archive  Artifact [6074f84ab7]

Artifact 6074f84ab7350f4420028f98bd1c582f9faa90cd01ce93d1fd519bd75db725ed:

  • File Get-Field.ps1 — part of check-in [9bf3e91c05] at 2018-06-10 13:04:34 on branch trunk — 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 (user: Andrew Savinykh size: 1246)

# encoding: ascii
# api: powershell
# title: Get-Field
# description: 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
# version: 0.1
# type: function
# author: Andrew Savinykh
# license: CC0
# function: Get-Field
# x-poshcode-id: 2057
# x-archived: 2016-01-25T19:22:25
# x-published: 2011-08-10T05:04:00
#
#
function Get-Field{
[CmdletBinding()]
	param ( 
		[Parameter(Position=0,Mandatory=$true)]
		$InputObject
	)
	
	$type = $InputObject.gettype()
	
	$publicNonPublic = [Reflection.BindingFlags]::Public -bor [Reflection.BindingFlags]::NonPublic
	$instance = $publicNonPublic -bor [Reflection.BindingFlags]::Instance
	$getField = $instance -bor [Reflection.BindingFlags]::GetField
	
	$fields = $type.GetFields($instance)
	
	$result = @{}
	$fields | Foreach-Object { $result[$_.Name] =  $type.InvokeMember($_.Name, $getField, $null, $InputObject, $null) } 
	
	$result
	
}

##Example:
##$context = (Get-Field $ExecutionContext)._context
##$context
##Get-Field $context
##$sessionState = (Get-Field $context)._enginesessionstate
##$sessionState
##$moduleTable = (Get-Field $sessionState)._moduleTable
##$moduleTable