Link to home
Start Free TrialLog in
Avatar of Mauro Cazabonnet
Mauro CazabonnetFlag for United States of America

asked on

Powershell pass array of objects to another script

Hi,
Trying to pass an object array to another script as follows

script I'm calling script1.ps1
param(
    [Parameter(ValueFromPipeline = $true)]
    [object[]]$test,
    [string]$batch
)

$host.ui.RawUI.WindowTitle = "Processing batch $batch "
$test | Out-File C:\work\test2.txt -Append
start-sleep -seconds 5

exit

Open in new window

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


main script calling script above

function Invoke-Command() {
    param ( [string]$program = $(throw "Please specify a program" ),
            [string]$argumentString = "",
            [switch]$waitForExit )

    $psi = new-object "Diagnostics.ProcessStartInfo"
    $psi.FileName = $program 
    $psi.Arguments = $argumentString
    $proc = [Diagnostics.Process]::Start($psi)
    if ( $waitForExit ) {
        $proc.WaitForExit();
    }
}

Open in new window

csv file --> test.csv
name,column1,column2,column3,column4
test,a,b,c,d
test1,e,f,g,h
test2,i,j,k,l

Open in new window

$importedcsv = import-csv .\test.csv
$batch = 1
Invoke-Command -program "powershell.exe" -argumentString "C:\work\script1.ps1 @($importedcsv) $batch" -waitForExit

Open in new window


the array param is not outputting to the file on the script script.ps1

Any help much appreciated....
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mauro Cazabonnet

ASKER

OK, this is good,
How do I get it to spawn a command shell showing what it's doing...
Avatar of oBdA
oBdA

It's doing that right inside the main Powershell console. Try the scripts I posted above, and you'll see the csv from script1.ps1 in yellow.
This should work yes?

$hvds = import-csv c:\work\pno-prod-combox35-12-05-16.csv


$batch = 1

Start-Process Powershell -ArgumentList "&C:\work\APATBuildEngine-Work.ps1 $batch $hvds"

Open in new window

Again: why are you complicating things by calling an external process, instead of starting the the Powershell script directly from the current session?
And note that your batch and array arguments are in a different order than in your sample script, and they're unnamed. Best practice in Powershell is to use named arguments.
$hvds = import-csv c:\work\pno-prod-combox35-12-05-16.csv

$batch = 1

& 'C:\work\APATBuildEngine-Work.ps1' $batch $hvds

Open in new window

I have a list that I then break down into batches and send to the powershell script
Your right why over complicate

Thanks again.....