pass variable with array data to new variable

This topic contains 7 replies, has 3 voices, and was last updated by  Max Kozlov 2 years, 3 months ago.

  • Author
    Posts
  • #26413

    Grant Harrington
    Participant

    I've got a function that accepts input (param) and based on the input, runs a switch loop.

        
    param(
            [Parameter(Mandatory=$TRUE)]
            [string[]]$myparam
            )
        
    switch ($myparam) {
    a {$y = $existingarray}
    b{$w = read-host "enter array name";$y = "`$$w"}
    } 
    
    foreach ($z in $y) {
    dosomething
    } 

    function01 -myparam a
    the values in the $existingarray loop through the foreach fine.

    function01 -myparam b
    Enter array: existingarray
    (would like $y = $existingarray)

    I guess I'm asking how to take the name of a variable that contains an array of data, and pass the contents of that array to a new variable (which can act as the 'existingarray' does).

  • #26437

    Rob Simmers
    Participant

    Let's look at how a function works in Powershell. In your parameters, you are specifying a parameter called $myparam and stipulating that it be mandatory for the function to work. If we create a quick test function:

    function Test-It {
        param(
            [Parameter(Mandatory=$TRUE)]
            [string[]]$myparam
        )
    
        begin{}
        process{
            foreach( $param in $myparam ) {
                "Processing {0}" -f $param
            }
        }
        end{}
    }
    

    If we pass an array, it would process it:

    $array = "Red", "Blue", "Green"
    Test-It -myparam $array
    

    results in:

    Processing Red
    Processing Blue
    Processing Green
    

    However, if you just call Test-It and do not pass a parameter, Powershell knows it's mandatory and will automatically prompt for input:

    PS C:\Users\Rob> Test-It
    cmdlet Test-It at command pipeline position 1
    Supply values for the following parameters:
    myparam[0]: Yellow
    myparam[1]: Blue
    myparam[2]: Purple
    myparam[3]: 
    Processing Yellow
    Processing Blue
    Processing Purple
    
  • #26439

    Grant Harrington
    Participant

    Thanks, I understand how the parameters work in a function.

    $existingarray = 'value01','value02'
    function my-array {
    
       Param(    [Parameter(Mandatory=$TRUE)]
            [string[]]$myparam
            )
        
    switch ($myparam) {
    a {$y = $existingarray}
    b{$w = read-host "enter array name";$y = "`$$w"}
    } 
    
    foreach ($z in $y) {
    "dosomething $z"
    }
    }
    

    my-array -myparam a
    dosomething value01
    dosomething value02

    my-array -myparam b
    enter array name: myparam
    dosomething $myparam

    I'd like the results of:
    my-array -myparam b
    enter array name: myparam

    to be:
    dosomething value01
    dosomething value02

  • #26442

    Max Kozlov
    Participant

    Just use "Get-Variable"

    PS C:\Users> $s = 1,2,3,4
    PS C:\Users> get-variable s

    Name Value
    ---- -----
    s {1, 2, 3, 4}

    PS C:\Users> (get-variable s).Value
    1
    2
    3
    4

  • #26449

    Grant Harrington
    Participant

    Thanks Max.

    That was what I was looking for. Below is how I implemented it in my actual code and it works great. Thanks!

    		switch ($InputType)
    		{
    			Array { $ArrayToRead = Read-Host "Enter Array";$GV = Get-Variable -name $ArrayToRead; $PCList = $GV.Value }
    			TextFile { $TextFile = Read-Host "Enter Text File path"; $PCList = Get-Content $TextFile }
    			StaticList { $PCList = $StaticList }
    		} #end switch InputType
    
  • #26451

    Rob Simmers
    Participant

    If you are pre-creating arrays to provide options, why not use validateset:

    function Test-It {
        param
            (
            [parameter(Mandatory=$true)]
            [ValidateSet("DEV", "QA", "PROD")]
            [String[]]
            $ServerPool
        ) 
    
        begin{
            switch ($ServerPool) {
                "DEV"{$servers = "devsrv01", "devsrv02", "devsrv03"}
                "QA"{$servers = "qasrv01", "qavsrv02", "qavsrv03"}
                "PROD"{$servers = "prdvsrv01", "prdsrv02", "prdsrv03"}
            }
        
        }
        process{
            foreach ($server in $servers) {
                "Processing {0}" -f $server
            }
    
        }
        end{}
    }
    
  • #26452

    Grant Harrington
    Participant

    Rob, I've got two different functions that produce data arrays, all with a unique name.

    I only need one of them to run in the third function, so I guess I could produce Array01, Array02 as switch keys, but this provides the flexibility to pass any array into the third function which is a test-connection script that saves online PCs to a new array variable to be passed along to additional scripts.

    My two scripts grab and parse PC names from either ADUC or DHCP Lease records, adds them to an array, passes them to Test-connection and if online sends to a new variable (or optionally outputs to text file), and then those PCs that are online get passed to several different functions depending on what data I need reported on a given online PC.

    Thanks for your example.

  • #26453

    Max Kozlov
    Participant

    another possible option (require some tuning 🙂 )

    function Test-It {
    param(
    	[Parameter(Mandatory=$true, ParameterSetName='array')]
    	[array]$Array,
    	[Parameter(Mandatory=$true, ParameterSetName='textfile')]
    	[string]$TextFile,
    	[Parameter(Mandatory=$true, ParameterSetName='default')]
    	[switch]$UseDefaultList
    )
    $StaticList = 'd','e','f','a','u','l','t'
    
    switch ($PsCmdlet.ParameterSetName)
    {
    	'array' { $PCList = $Array }
    	'textfile' { $PCList = Get-Content $TextFile }
    	default { $PCList = $StaticList }
    }
    $PCList
    }
    '---'
    Test-It -Array 1,2,3,4
    '---'
    Test-It -TextFile d:\111
    '---'
    Test-It -UseDefaultList
    

You must be logged in to reply to this topic.