Author Posts

December 27, 2017 at 9:48 am

Hi Team,
I get confused with the functionality of an array and variable parameter. Please explain me the difference /advantage of one on other below;

$serverList= @()
$serverList= ("Testserver1","Testserver2","Testserver3")

and

[hashtable]$Name= @{"serverList" = $null; "serverListCount" = $null}
$serverList= ("Testserver1","Testserver2","Testserver3")
$Name.serverList = $serverList

Thanks,
Kishor

December 27, 2017 at 12:58 pm

They're not the same thing.
The former is an array. This is a "data structure that is designed to store a collection of items".

The latter is a hash table, which is a different data structure that holds key-value pairs.

December 27, 2017 at 3:41 pm

$serverList = 'Testserver1','Testserver2','Testserver3'                                                       
$serverList.GetType()                                                                                         

IsPublic IsSerial Name                                     BaseType                                                            
-------- -------- ----                                     --------                                                            
True     True     Object[]                                 System.Array                                                        


$Name= @{serverList = $null; serverListCount = $null}
$Name.GetType()                                                                                               

IsPublic IsSerial Name                                     BaseType                                                            
-------- -------- ----                                     --------                                                            
True     True     Hashtable                                System.Object                                                       

December 27, 2017 at 11:45 pm

A PowerShell array holds a list of data items. The data elements of a PowerShell array need not be of the same type, unless the data type is declared (strongly typed). Arrays are built using operators or casts. The way to build an array in PowerShell is to use the comma operator.

A variable parameter is use when you need to store a static value or the results of some other calculation/collection/operation. One can pass an array data as a parameter or even a named parameter. For Example:

    Function Show-Friends
    {
        [CmdletBinding()]

        Param
        (
            [string] $MainPerson, 
            [string[]] $Friends
        )

        "Main person is ",$MainPerson
        'Friends count is : ' ,$Friends.Count

        foreach($Friend in $Friends)
        {
            $MainPerson + " has a Friend named: ” ,$Friend
        }

    }


    Show-Friends
    Show-Friends -MainPerson:Smith  -Friends:@('Jon', 'Joshua')
    Show-Friends Smith ('Jon', 'Joshua')

However, your second block is not a 'variable parameter' as noted by you, it's a hastable. Hash Tables (also known as Associative arrays or Dictionaries) are a type of array that allows the storage of paired Keys and Values, rather like a simple database table. Unlike normal arrays where you refer to each element via a numeric index, the keys of a hash table can be strings. The Key values do still need to be unique, if you try to add the same key twice PowerShell will return an error ("item has already been added").

For a review regarding variable and parameters, then look here:

About Functions Advanced Parameters
'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1'

Simplify Your PowerShell Script with Parameter Validation
'blogs.technet.microsoft.com/heyscriptingguy/2011/05/15/simplify-your-powershell-script-with-parameter-validation'

PowerShell – Passing Parameters as Variables using Remote Management and Invoke-Command
'blogs.technet.microsoft.com/ralphkyttle/2015/06/04/powershell-passing-parameters-as-variables-using-remote-management-and-invoke-command'

Use the Get-Command PowerShell Cmdlet to Find Parameter Set Information
'blogs.technet.microsoft.com/heyscriptingguy/2012/05/16/use-the-get-command-powershell-cmdlet-to-find-parameter-set-information'

Use Parameter Sets to Simplify PowerShell Commands
'blogs.technet.microsoft.com/heyscriptingguy/2011/06/30/use-parameter-sets-to-simplify-powershell-commands'

Weekend Scripter: Use PowerShell to Find Dynamic Parameters
'blogs.technet.microsoft.com/heyscriptingguy/2013/03/09/weekend-scripter-use-powershell-to-find-dynamic-parameters'

Dynamic ValidateSet in a Dynamic Parameter
'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1

However, your post title is 'Add to array'

Here is a good write up on add / remove reltive to arrays
'jonathanmedd.net/2014/01/adding-and-removing-items-from-a-powershell-array.html'

December 28, 2017 at 6:53 am

Thank you all and especially Postanote for detailed explanation...I am happy now, heading further deep into understanding this :-)!!!