Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: passing parameters to a job

I have a script that requires a number of parameters:

param ([string]$FOO="foo",[string]$CFG='\ps\bcpCopyCfg.ps1', [string]$CFROM="none", `
[string]$CTO="none", [switch]$HELP=$FALSE, [switch]$FULL=$FALSE, [string]$CCOL="none" `
,[string]$CDSQUERY="none", [string]$CMSSRV="none" `
,[string]$CSYBDB="none", [string]$CMSDB="none")

when called from the command prompt e.g.

powershell .\bcpCopy.ps1 -CFROM earn_n_deduct_actg -CTO fin_earn_n_deduct_actg -CCOL f_edeh_doc_id

everything works fine. I need however to start several (dozens) instances of the script in parallel and I've written a wrapper script that calls the one doing the actual work as a job: I prepare an array with the arguments (including the keywords like "-CFG") anhd pass it to start-job:

    # Prepare script block to be released
    $ARGS=("-CFG ", $CFG, "-CSYBDB ", $SYBDB, "-CMSDB ",$MSDB, "-CFROM ", $SYBTBL, "-CTO ",$MSTBL)
    if ($FULL) {
        $ARGS = $ARGS + " -FULL"
    } else { 
        $ARGS = $ARGS + " -CCOL $($args[5])  "
    } 
    "Argument array:"
    $ARGS

    start-job  -scriptblock {powershell.exe -file '\ps\bcpCopy.ps1'} -ArgumentList $ARGS

Unfortunately, the called script does not receive the arguments: the caller prints the array and it looks fine:

Argument array:
-CFG
\ps\bcpCopyCfgOAH.ps1
-CSYBDB
vnimisro
-CMSDB
IMIS_UNOV
-CFROM
earn_n_deduct_ref
-CTO
fin_earn_n_deduct_ref
-FULL

but the output from the called scripts says that the only parameter received is the configuration file -- all the rest are at their default values.

PS C:\ps> receive-job 1391
12/17/2010 10:54:14 Starting the upload of table none; source db none;
12/17/2010 10:54:14 Target table is none; target db is none;
12/17/2010 10:54:14 Config file is \ps\bcpCopyCfg.ps1.
12/17/2010 10:54:14 Target server (MS SQL) is secap900-new
12/17/2010 10:54:14 Source database must be specified. Exiting...

Can you please point me what am I doing wrong?

like image 737
Lubo Avatar asked Dec 17 '10 15:12

Lubo


People also ask

How do you pass parameters to a function in PowerShell?

How do I pass parameters to PowerShell scripts? Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value.

How do I pass multiple parameters to a PowerShell script?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do you pass a variable in a PowerShell script?

Use param to Pass an Argument to a PowerShell Script We can define arguments using the param statement. It also allows us to use the default values. Here, the variable $address has a default value. And the default value will be used if the user does not provide any value.

What is param () in PowerShell?

Parameters can be created for scripts and functions and are always enclosed in a param block defined with the param keyword, followed by opening and closing parentheses. param() Inside of that param block contains one or more parameters defined by -- at their most basic -- a single variable as shown below.


1 Answers

I'm not sure what exactly you're trying to do, but this looks wrong:

start-job  -scriptblock {
    powershell.exe -file '\ps\bcpCopy.ps1'} -ArgumentList $ARGS

You are creating an entirely new powershell process needlessly. Try this instead:

start-job  -scriptblock {
    & 'c:\ps\bcpCopy.ps1' @args } -ArgumentList $ARGS

The "@args" syntax is called "splatting." This will expand the passed arguments and ensure each element is treated as a parameter. The ampersand (&) is the "call" operator.

like image 65
x0n Avatar answered Sep 24 '22 15:09

x0n