本文介绍了PowerShell的:将参数传递给工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要许多参数的脚本:

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

一切工作正常。
然而,我要开始数(几十个)并行脚本的情况下,我已经写了调用一个做实际工作作为作业的包装脚本:
我prepare与参数(包括像-cfg的关键字)一个数组anhd通过它来启动工作:

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:

参数数组:结果
-cfg结果
\\ PS \\ bcpCopyCfgOAH.ps1结果
-CSYBDB结果
vnimisro结果
-CMSDB结果
IMIS_UNOV结果
-CFROM结果
earn_n_deduct_ref结果
-CTO结果
fin_earn_n_deduct_ref结果
 -full

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>接收工作1391结果
  12/17/2010 10时54分14秒开始表没有任何的上传;源DB无;结果
  12/17/2010 10时54分14秒目标表是没有。目标数据块是没有。结果
  12/17/2010 10时54分14秒的配置文件是\\ PS \\ bcpCopyCfg.ps1。结果
  12/17/2010 10时54分14秒的目标服务器(MS SQL)是secap900全新结果
  12/17/2010 10时54分14秒源数据库必须指定。退出...

您可以请点我什么我做错了?

Can you please point me what am I doing wrong?

推荐答案

我不知道你想要做的事情,但是这看起来错了:

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

您不必要创建一个全新的PowerShell进程。试试这个:

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

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

在@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.

这篇关于PowerShell的:将参数传递给工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:00