Archive for March, 2016

 

If you are familiar with Powershell scripting, you surely know that variables can really mess up your day.

I’ve written a little function you can dot source into your script and run at specific points in your script to show the current variables and their types and values. It’s not perfect, and I’ll probably make it prettier in time. But here you go.

newdl

 


 Function CheckMyVariables
 {
    Param
        ([Switch]$Full)

    Function CheckVariableType
    {
        Param
            ($Inputdata,$Inputname)

        Begin
        {
            Write-Host ""
            Write-Host -ForegroundColor DarkGray "*Started*"
        }

        Process
        {
            #Check what kind of type the variable is and if it is empty
            If(!($InputData)){Write-Host -ForegroundColor Yellow "The variable $Inputname is Null or Empty ";Return}
            If($InputData -is [String] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is a: " -NoNewline;Write-Host "[String]"}
            Elseif ($InputData -is [Int] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Int]"}
            Elseif ($InputData -is [Array] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Array]"}
            Elseif ($InputData -is [System.Boolean] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Boolean]"}
            Elseif ($InputData -is [System.Collections.Arraylist] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Arraylist]"}

            #Formatting output of Arrays and Arraylists
            If($InputData -is [Array] -eq $true -or $InputData -is [System.Collections.Arraylist] -eq $true)
            {
                Write-Host -ForegroundColor Green ""$"$Inputname value(s) is/are:"
                $InputData
            }

            #Normal formatting on everything else
            Else
            {
                Write-Host -ForegroundColor Green ""$"$Inputname value(s) is/are: " -NoNewline
                Write-Host $InputData
            }

            #If switch is enabled then run the following
            If($Full -eq $true)
            {
                Write-Host -ForegroundColor Green ""$"$Inputname type confirmed with .GetType() cmlet: " -NoNewline
                Write-Host $InputData.GetType()
            }
        }

        End
        {
            Write-Host -ForegroundColor DarkGray "*Finished*"
        }
    }

    #A function to remove multiple values from an array
    Function RemoveArrayValues ($Array,$RemoveValues)
    {
        Foreach ($value in $RemoveValues)
        {
            $Array = $Array | ? {$_ -ne $value}
        }
        Return $Array
    }

    #Here I list environment variables and other variables i want to exclude from view..
    $ExcludeVar = '$','?','^','args','ConfirmPreference','ConsoleFileName','DebugPreference','Error','ErrorActionPreference','ErrorView','ExecutionContext','false','FormatEnumerationLimit',
    'HOME','Host','InformationPreference','input','MaximumAliasCount','MaximumDriveCount','MaximumErrorCount','MaximumFunctionCount','MaximumHistoryCount','MaximumVariableCount','MyInvocation',
    'NestedPromptLevel','null','OutputEncoding','PID','profile','ProgressPreference','PSBoundParameters','PSCommandPath','PSCulture','PSDefaultParameterValues','PSEmailServer','PSHOME','psISE',
    'PSScriptRoot','PSSessionApplicationName','PSSessionConfigurationName','PSSessionOption','PSUICulture','psUnsupportedConsoleApplications','PSVersionTable','PWD','ShellId','StackTrace',
    'true','VerbosePreference','WarningPreference','WhatIfPreference','x64','foreach','ExcludeVar','checkvar','var','allvariables','Full'

    #Get all variables
    $allVariables = (Get-Variable).Name

    #Remove excludevariableslist
    $allVariables = RemoveArrayValues $allVariables $ExcludeVar
    Foreach ($var in $allVariables)
    {
        $checkvar = Get-Variable -Name $var
        CheckVariableType $checkvar.Value $var
    }

    #Default value of the switch is false
    $Full = $false
}

Run as follows


CheckMyVariables