Created
October 14, 2022 14:46
-
-
Save LaurentDardenne/ca98f56afdabe4d6318002f342b404cc to your computer and use it in GitHub Desktop.
Get-VariableType : Workaround to get the type of PSVariable when it s $null
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-VariableType { | |
#If a PSVariable is $null, it is impossible to get his type. | |
#One must read the _convertTypes member of ArgumentTypeConverterAttribute class. | |
#Origin : http://poshcode.com/998 | |
param([string]$Name) | |
Write-Debug ("Call : {0}" -F $MyInvocation.InvocationName) | |
get-variable $name | select -expand attributes | ? { | |
$_.gettype().name -eq "ArgumentTypeConverterAttribute" } | % { | |
$_.gettype().invokemember("_convertTypes", "NonPublic,Instance,GetField", $null, $_, @()) | |
} | |
Write-Debug ("End : {0}" -F $MyInvocation.InvocationName) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment