PowerShell如果对ExecuteSSHCommand为null,如何使参数可选而不添加参数

时间:2013-09-18 19:17:52

标签: powershell powershell-v2.0 powershell-v3.0

我有一个PowerShell脚本,其功能如下:

function RunOtherScript($_oneParameter, $twoParameter)
{
    ExecuteSSHCommand ("python -u otherScript.py " +
                       "--oneParameter $_oneParameter " +
                       "--twoParameter $twoParameter ")
}

参数“twoParameter”是可选的。我的问题是如何根据$ twoParameter是否为空来添加字符串“--twoParameter $ twoParameter”

3 个答案:

答案 0 :(得分:3)

我会这样做:

function RunOtherScript($_oneParameter, $twoParameter) {
  $cmd = "python -u otherScript.py --oneParameter $_oneParameter"
  if ($twoParameter -ne $null) { $cmd += " --twoParameter $twoParameter" }
  ExecuteSSHCommand $cmd
}

答案 1 :(得分:0)

哈哈,没有简单的方法可以解决这个问题:

function RunOtherScript($_oneParameter, $twoParameter)
{
    if($twoParameter -ne $null){
        ExecuteSSHCommand ("python -u otherScript.py " +
                           "--oneParameter $_oneParameter " +
                           "--twoParameter $twoParameter ")
    }

    else
        ExecuteSSHCommand ("python -u otherScript.py " +
                           "--oneParameter $_oneParameter ")
}

答案 2 :(得分:0)

注意:@AnsgarWiechers answer是建议的答案。

function RunOtherScript($_oneParameter, $twoParameter) {
    $cmd = "python -u otherScript.py --oneParameter $_oneParameter"
    if ($twoParameter) { $cmd += " --twoParameter $twoParameter" }
    ExecuteSSHCommand $cmd
}

以下内容纯粹是为了学术兴趣而发布的。

这是一个解决方案;但它只适用于数字/仅仅是为了兴趣,而不是一个好的或推荐的解决方案:

(我在下面以不同的方式对待oneParametertwoParameter;如果oneParameter不存在,则python仍会指定它的调用(只是没有传递值);如果{{1虽然该参数在python调用中没有显示,但是遗漏了。

twoParameter

输出

cls
function RunOtherScript($_oneParameter, $twoParameter)
{
    ("python -u otherScript.py --oneParameter {0} {1:--twoParameter 0}" -f $_oneParameter, $twoParameter)
}

RunOtherScript
RunOtherScript "a"  
RunOtherScript "a" 10
RunOtherScript "a" "10" # this doesn't work the way you'd want it to

另一个替代:

这不是一个黑客,但需要更多的代码;如果您有很多参数,那么可能是值得的:

python -u otherScript.py --oneParameter  
python -u otherScript.py --oneParameter a 
python -u otherScript.py --oneParameter a --twoParameter 10
python -u otherScript.py --oneParameter a 10

输出:

clear-host
function RunOtherScript{
 [CmdletBinding()]
    Param(
        [parameter(Mandatory = $true)]
        [string]$_oneParameter
        ,
        [parameter(ValueFromRemainingArguments = $true)]
        [String] $args
    )
    process {
        [string]$cmd = "python -u otherScript.py"
        $cmd = $cmd + " --oneParameter $_oneParameter"
        $args | ?{$_ -ne ""} | %{ $cmd = $cmd + " --twoParameter $_" }
        write-output $cmd #to show what we'd be executing
        #ExecuteSSHCommand $cmd
    }
}
RunOtherScript "one" "two" "three"
RunOtherScript "one" "two" 
RunOtherScript "one"

或许多参数:

python -u otherScript.py --oneParameter one --twoParameter two three
python -u otherScript.py --oneParameter one --twoParameter two
python -u otherScript.py --oneParameter one

输出:

clear-host
function RunOtherScript{
 [CmdletBinding()]
    Param(
        [parameter(Mandatory = $true)]
        [string]$_oneParameter
        ,
        [parameter(ValueFromRemainingArguments = $true)]
        [string[]]$arguments
    )
    begin {
        [string[]]$optionalParams = 'twoParameter','threeParameter','fourParameter'
    }
    process {
        [string]$cmd = "python -u otherScript.py"
        $cmd = $cmd + " --oneParameter $_oneParameter"
        [int]$i = -1
        $arguments | ?{($_) -and (++$i -lt $optionalParams.Count)} | %{ $cmd = $cmd + " --" + $optionalParams[$i] + " " + $arguments[$i] }
        write-output $cmd 
        #ExecuteSSHCommand $cmd
    }
}
RunOtherScript "one" "two" "three" "four" "five"
RunOtherScript "one" "two" "three" "four"
RunOtherScript "one" "two" "three"
RunOtherScript "one" "two" 
RunOtherScript "one"
相关问题