Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Poweshell array confusion Expand / Collapse
Author
Message
Posted Tuesday, October 22, 2013 7:42 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 17, 2014 4:16 AM
Points: 13, Visits: 104
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



Post #1507147
Posted Tuesday, October 22, 2013 7:56 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 1:04 PM
Points: 1,336, Visits: 743
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
Post #1507155
Posted Tuesday, October 22, 2013 8:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 17, 2014 4:16 AM
Points: 13, Visits: 104
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
Post #1507189
Posted Tuesday, October 22, 2013 9:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:41 AM
Points: 5,204, Visits: 2,837
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!!!
Post #1507216
Posted Tuesday, October 22, 2013 12:38 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 1:04 PM
Points: 1,336, Visits: 743
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
Post #1507288
Posted Wednesday, October 23, 2013 2:04 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 17, 2014 4:16 AM
Points: 13, Visits: 104
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
Post #1507489
Posted Wednesday, October 23, 2013 2:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:41 AM
Points: 5,204, Visits: 2,837
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!!!
Post #1507490
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse