2

I would like to access all PSVariables set within a commandlet in C#. So, my one C# Commandlet would set variables from powershell script in sessionState and another commandlet will enumerate all sessionState variables along with their types set within commandlets. I don't want to enumerate script variables.

e.g.

[string[]]$ListOfEmployees = "EmployeeA","EmployeeA","EmployeeC"
Set-Parameters "Employees" $ListOfEmployees

[string[]]$ListOfDepartments = "Communication","Mathematics","ComputerScience"
Set-Parameters "Departments" $ListOfDepartments 

[string[]]$ListOfCoursesInAllDepts = "CourseA","CourseB","CourseC"
Set-Parameters "Courses" $ListOfCoursesInAllDepts 

and later at certain point in the script i would like to access exactly above variables along with their types.

i.e.

$availableVariables = Enumerate-AllAvailableVariables

and the output of above Commandlet should be

Name             Type
-------------    ------
Employees        String[]
Departments      String[]
Courses          String[]

How would that be possible?

Usman
  • 2,742
  • 4
  • 44
  • 82
  • Why would you fill up variables with values if your output shows only property names and its type? – Kirill Pashkov Jun 15 '18 at 10:53
  • Because the output of above PSComndlet is only to enumerate available variables. Basically for my business use case, it is required to facilitate user to see available variables in powershell at any instant. – Usman Jun 15 '18 at 11:15
  • My one business case is to populate powershell variables by reading external file from the disk. ( This might contains 1000s of variables with values ) and user might be interested in getting values of just of few of them. So one commandlet can enumerate only the names and types of loaded variables – Usman Jun 15 '18 at 11:17
  • I think you want to use automated variable $PSBoundParameters – Kirill Pashkov Jun 15 '18 at 11:25
  • Unfortunately, not. $PSBoundParameters just contains the dictionary of the parameters that are passed to a script or function and their current values. Where as I clearly written what I want. – Usman Jun 15 '18 at 11:41

1 Answers1

1

I think you are looking for automated variable $PSBoundParameters which is captures input parameters and their values etc.

While I am still not 100% sure what exactly you are looking for I will try to suggest you a couple of advices.

Here's a code samples I just made:

By this one I hope you would get the point of how $PSBoundParameters works.

function Do-This
{
    param(
        [String[]]$Employees,
        [String[]]$Departments,
        [String[]]$Courses
    )
    $Keys = $PSBoundParameters.Keys
    foreach ($Key in $Keys)
    {
        New-Object PSObject -Property @{
            Name=$Key
            Type=$PSBoundParameters[$Key].GetType()
        }
    }
}
Do-This -Employees @('EmployeeA','EmployeeA','EmployeeC') -Departments @('Communication','Mathematics','ComputerScience') -Courses @('CourseA','CourseB','CourseC')

There is also a great way to get function information such as parameter names and their types - Get-Help cmdlet. Here is a function i've made based on it's output.

function Get-FunctionParams
{
param([Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)][string]$FunctionName)
    $Results = @()
    $Parameters = @((Get-Help $FunctionName).parameters.parameter)
    if ($Parameters.Count -eq 0)
    {
        Write-Host $('Function {0} has no parameters' -f $FunctionName)
        break
    }
    foreach ($Parameter in $Parameters){
        $Object = New-Object -TypeName PSObject
        $Object | Add-Member -MemberType NoteProperty -Name Name -Value $Parameter.name
        $Object | Add-Member -MemberType NoteProperty -Name aliases -Value $Parameter.aliases
        $Object | Add-Member -MemberType NoteProperty -Name required -Value $Parameter.required
        $Object | Add-Member -MemberType NoteProperty -Name type -Value $Parameter.type.name
        $Object | Add-Member -MemberType NoteProperty -Name position -Value $Parameter.position
        $Object | Add-Member -MemberType NoteProperty -Name defaultValue -Value $Parameter.defaultValue
        $Object | Add-Member -MemberType NoteProperty -Name parameterSetName -Value $Parameter.parameterSetName
        $Results += $Object
    }
    Write-Output $Results | Sort-Object position,Name | Format-Table -AutoSize -Wrap
}

Usage example Get-FunctionParams -FunctionName Get-Process Output of usage example:

Name            aliases     required type      position   defaultValue parameterSetName
----            -------     -------- ----      --------   ------------ ----------------
Name            ProcessName false    string[]  0                       Name            
ComputerName    Cn          false    string[]  Имя задано              (Все)           
FileVersionInfo FV, FVI     false    switch    Имя задано              (Все)           
Id              PID         true     int[]     Имя задано              Id              
InputObject     Отсутствует true     Process[] Имя задано              InputObject     
Module          Отсутствует false    switch    Имя задано              (Все)       
Kirill Pashkov
  • 3,118
  • 1
  • 15
  • 20