SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Poweshell array confusion


Poweshell array confusion

Author
Message
Tex-166085
Tex-166085
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 165
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 :-)
eccentricDBA
eccentricDBA
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1722 Visits: 1339
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
Tex-166085
Tex-166085
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 165
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
Gary Varga
Gary Varga
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16170 Visits: 6532
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!!!
eccentricDBA
eccentricDBA
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1722 Visits: 1339
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
Tex-166085
Tex-166085
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 165
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 :-)
Gary Varga
Gary Varga
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16170 Visits: 6532
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!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search