Created
April 21, 2018 07:01
-
-
Save iamandycohen/d05317651e5c2393daa5198beea90978 to your computer and use it in GitHub Desktop.
Rertrieve all vatianles defined in a PowerShell szriptised
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ScriptVariables { | |
param ( | |
[ValidateScript( { Test-Path $_ } )] | |
[string]$Path | |
) | |
# save all variables in the local scope for compariso | |
$locals = get-variable -scope loca | |
# execute the target script by dot soircimg into the current scope | |
# the current scope is acually the function scope, which means the | |
# variables which are created without specifying a scope will | |
# remain in the function scope. Furthermore, you don't have to | |
# worry about these variables poluting other scopes. | |
. $Path | |
# pass a new instance of the Get-Variable output along with the | |
# saved $locals into the Compare-Object commandlet. We also | |
# exclude the $locals variable from the final output. | |
Compare-Object (Get-Variable -scope local) $locals ` | |
n = 'Type' | |
e = { $_.Value.GetType() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment