• Tex-166085 (10/22/2013)


    Hi

    I am new to powershell, and am struggling a bit with the passing of an array as a variable. Firstly, I have a function which will return a set of server names based on and variable for the environment. However, when I try to pass the output from the function to another script, it only returns data for the first value (I have set up my test for 3 servers to be returned). I must be passing the variable incorrectly, or scoping it incorrectly, but I am a bit stuck now. Can anyone tell me where I am going wrong, please?

    -----------------------------------------------------------------------------------------

    Function:

    Function GetComputerList{

    Param( [string]$Environment)

    $comparray = @()

    $query = "

    SELECT ServerName

    FROM Server S

    INNER JOIN

    dbo.Environment E

    ON S.EnvironmentID = E.EnvironmentID

    WHERE E.EnvironmentName = '$Environment'"

    $connection = new-object system.data.sqlclient.sqlconnection( "Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;”)

    $adapter = new-object system.data.sqlclient.sqldataadapter ($query, $connection)

    $table = new-object system.data.datatable

    $adapter.Fill($table) | out-null

    $compArray = @($table | select -ExpandProperty ServerName)

    return ($compArray)

    }

    ----------------------------------------------------------------------------

    Script 1:

    . \Powershell\GetComputerList.ps1

    set-location C:\Powershell

    #Write-Host "Calling CheckJavaProcesses"

    $ComputerArray = @()

    $ComputerArray = GetComputerList 'Prod'

    Write-host $ComputerArray

    #check AL_engine processes

    $Command = ".\CheckJavaProcesses.ps1 '$ComputerArray'"

    Invoke-Expression $Command

    --------------------------------------------------------------

    Script 2:

    param([string[]] $ComputerArray)

    $date = (get-date).tostring("yyyyMMddhhmm")

    $logfile = "C:\Powershell\Logs\PRD_CheckProcesses_$date.log"

    Write-Host $ComputerArray

    # Processes

    ForEach ($i in $ComputerArray ){$i >> $logfile; get-process -computername $i -name java* >> $logfile}

    Notepad $logfile

    Thanks for any help in advance 🙂

    I did a quick seach and found this. Let me know if it helps.

    start-job -filepath myscript.ps1 -arg (,$myarr)

    The -ArgumentList takes in a list/array of arguments. So when you give -arg $myarr, it is as though you are passing the elements of the array as the arguments. So you have to force PowerShell to treat it as a single argument which is an array.

    http://stackoverflow.com/questions/7152740/how-to-pass-an-array-as-a-parameter-to-another-script