Parameter variables

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5225

    by willsteele at 2012-10-23 08:54:04

    I need to check all of my parameters in a given function to see what is being used and what is not. Is there a variable I can examine rather than having to look at each parameter one by one?

    by DonJ at 2012-10-23 15:45:56

    Well, $PSBoundParameters has all bound parameters; this won't include any parameter which received pipeline input, though. I don't think. But you sort of still have to look at each one, right? It's a hashtable, so the keys are your parameter names: $PSBoundParameters.ContainsKey('whatever') would return $True if that parameter was used.

    See http://blogs.msdn.com/b/powershell/arch ... eters.aspx.

    by Jason_Yoder_MCT at 2012-10-23 16:58:13

    Willsteel

    Here is something to take a look at. There are two functions below.

    Test-Params is the one you are most interested in. It simply list all the parameters of a function and returns True or False. True if the parameter has a value.

    Test is a function with 3 parameters. It passes its parameter information to Test-Function.

    The calling statement only sends information to 2 of the parameters. You should get something like this in back:

    Parameter HasData
    ——— ——-
    A True
    b True
    c False

    What you do from here is up to you. Have fun!

    Function Test-Params
    {
    Param (
    $Set1,
    $Set2
    )
    ForEach ($Item2 in $Set2)
    {
    $Obj = New-Object -TypeName PSObject
    $Obj | Add-Member -MemberType NoteProperty -Name "Parameter" -Value $Item2
    $Obj | Add-Member -MemberType NoteProperty -Name "HasData" -Value $False

    ForEach ($Item1 in $Set1)
    {

    If ($Item1 -eq $Item2)
    {
    $Obj.HasData = $True
    }

    }
    Write-Output $Obj
    }

    }

    Function test
    {
    Param(
    [String]$A,
    [int]$b,
    [String]$c

    )

    Test-Params ($PSBoundParameters.keys) ((Get-Command test).Parameters).keys

    }

    test 'Hello' 5

    by Jason_Yoder_MCT at 2012-10-23 17:00:26

    Don,

    Like you answer better.

You must be logged in to reply to this topic.