Skip to content

Instantly share code, notes, and snippets.

@LaurentDardenne
Created October 14, 2022 14:46
Show Gist options
  • Save LaurentDardenne/ca98f56afdabe4d6318002f342b404cc to your computer and use it in GitHub Desktop.
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
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