Powershell: passing parameters to a job

前端 未结 2 1669
野性不改
野性不改 2021-01-06 00:31

I have a script that requires a number of parameters:

param ([string]$FOO=\"foo\",[string]$CFG=\'\\ps\\bcpCopyCfg.ps1\', [string]$CFROM=\"none\", `
[string]$         


        
相关标签:
2条回答
  • 2021-01-06 01:20

    Another way is to put the @ before the array. I changed the $ARGS variable to $flags to differentiate $args in the scriptblock from $flags.

    $flags = @("-CFG ", $CFG, "-CSYBDB ", $SYBDB, "-CMSDB ",$MSDB, "-CFROM ", $SYBTBL, "-CTO ",$MSTBL)
    
    If($FULL) {
        $flags = $flags + " -FULL"
    }
    Else {
       $flags = $flags + " -CCOL $($args[5])  "
    }
    "Argument array:"
    $flags
    
    start-job  -scriptblock {powershell.exe -file '\ps\bcpCopy.ps1' $args} -ArgumentList $flags
    
    0 讨论(0)
  • 2021-01-06 01:23

    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.

    0 讨论(0)
提交回复
热议问题