Poweshell array confusion

  • 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 🙂

  • 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

  • hi,

    thanks for your quick reply. I did try this, but now I get another error with the Invoke-Expression:

    Changed the command to :

    $Command = ".\CheckJavaProcesses.ps1 -arg (,$ComputerArray)"

    Error now is:

    Invoke-Expression : Missing expression after unary operator ','

    If I try to call the script without buliding the command first I get:

    The argument is null or empty. Supply an argument that is not null or empty and then try the command again

    I am probably being obtuse here, so apologies for that

  • Try using single quotes instead of double (variables get treated differently):

    $Command = '.\CheckJavaProcesses.ps1 -arg (,$ComputerArray)'

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Actually I believe the double qoute was your original problem. Bellow I have a simplified example. A double quote is going to cause the variable to be evaluated when you create the command script.

    PassArray.ps1

    param([string[]] $ComputerArray)

    ForEach ($Computer in $ComputerArray ){

    Write-Host $Computer

    }

    CallPassArray.ps1

    $ComputerArray = @('computer1','computer2')

    $Command = 'C:\work\PassArray.ps1 $ComputerArray'

    Invoke-Expression $Command

    When executed it will return the following:

    PS M:\> C:\work\CallPassArray.ps1

    computer1

    computer2

  • Thank you so much, it works perfectly with single quotes!

    I will have to read up about the difference between single and double quotes

    Thanks to you both, once again 🙂

  • You're welcome.

    Search for MS' PowerShell cheat sheets and starter guides (usually PDFs and docx). There are a couple they have provided that are quite useful.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply