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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With