Trace-Variable.ps1
<#PSScriptInfo .VERSION 1.0.0.0 .GUID be5c9162-220a-4d14-a118-97302a7ac71d .AUTHOR Jeffrey Snover .COMPANYNAME Microsoft .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION Displays the variables from each stack in the callstack to facilitate script debugging #> Param( [Parameter()] [String[]]$SkipList = $("?","args","ConfirmPreference","ConsoleFileName","DebugPreference","ErrorActionPreference","ErrorView","ExecutionContext","false", "FormatEnumerationLimit","Home","Host","InformationPreference","input","MaximumAliasCount","MaximumDriveCount", "MaximumErrorCount","MaximumFunctionCount","MaximumHistoryCount","MaximumVariableCount","MyInvocation","NestedPromptLevel","null", "OutputEncoding","Pid","PSBoundParameters","PSEdition","PSHome","PSScriptRoot","ProgressPreference","PSCmdlet","PSCommandPath", "PSCulture","PSEmailServer","PSHOME","PSUICulture","PSVersionTable","ShellId","true","VerbosePreference", "WarningPreference","WhatIfPreference") ) $StackTrace = Get-PSCallStack $line = "*" * 80 "{0}`n**** Tracing Variables from Stackframes:`n{2}" -f $line, $($StackTrace.count - 1), $line for ($i = 1; $i -le $StackTrace.count - 1 ; $i++) { $v = Get-Variable -Scope $i $vgoodname = $v.Where{$_.name -NotIn $SkipList} $vdiff = Compare-Object $v (Get-Variable -Scope $($i+1)) -Property Name,Value | where {$_.SideIndicator -eq "<=" -and $_.Name -notin $skipList} "Variables from Function [{0}] at Location [{1}]{2}" -f $StackTrace[$i].Command, $StackTrace[$i].Location, $($vdiff |Format-Table name,value -wrap|out-string -Width 1024) } |