将参数Array传递给powershell.exe [英] Passing parameter Array to powershell.exe

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

问题描述

我有一个脚本,需要2个数组作为输入,以及可选的日志名:

I have a script that require 2 arrays as input, and optional logname :

#file:test.ps1
Param(
    [string[]]$array1,
    [string[]]$array2,
    [string]$logName = "log{0}.csv" -f (get-date -format "yyyyMMdd")
)

"array1: {0}" -f ($array1 -join " ")
"array2: {0}({1})" -f ($array2 -join " ") ,$array2.count
"logName: {0}" -f $logName

从PowerShell控制台运行时,一切都很好:

When run from a PowerShell console, everything is fine:


PS D:\temp> .\test.ps1 -array1 one,two -array2 1,2,3
array1: one two
array2: 1 2 3(3)
logName: log20190723.csv

但是,通过调用powershell.exe(用于计划任务)运行时,仅会捕获数组中的第一个元素,其余的将传递给logname参数.

But when run by calling powershell.exe (for scheduled task), only first element from array is grabbed, the rest is passed to the logname parameter.


PS D:\temp> powershell.exe -F D:\temp\test.ps1 -array1 one,two -array2 1,2,3
array1: one
array2: 1(1)
logName: two

我应该如何定义参数以将所有参数获取到数组中?

How should I define params to grab all params into the array?

(顺便说一句:我使用的是PS4.0,在Windows 2008和2012上的结果相同)

(BTW: I'm using PS4.0, with same result on Windows 2008 and 2012)

推荐答案

TL; DR:您不能跨进程边界传递PowerShell数组.

TL;DR: You cannot pass PowerShell arrays across process boundaries.

脚本的第一次调用在当前的PowerShell进程中运行,因此one,two1,2,3作为PowerShell数组传递.

The first invocation of your script runs within the current PowerShell process, hence one,two and 1,2,3 are passed as PowerShell arrays.

脚本的第二次调用将启动第二个PowerShell进程作为外部程序,从而绕道而行,然后重新进入PowerShell.因此,由于Windows对PowerShell数组一无所知,因此one,two1,2,3作为字符串而不是作为数组传递给第二个PowerShell进程.

The second invocation of your script launches a second PowerShell process as an external program, thus taking a detour leaving and re-entering PowerShell. Because of that one,two and 1,2,3 are passed to the second PowerShell process as strings, not as arrays, since Windows doesn't know anything about PowerShell arrays.

您可以在脚本开头以逗号分隔参数值以减轻此限制:

You could split the parameter values at commas at the beginning of your script to mitigate this limitation:

Param(
    [string[]]$array1,
    [string[]]$array2,
    [string]$logName = "log{0}.csv" -f (Get-Date -Format "yyyyMMdd")
)

if ($array1.Count -eq 1) { $array1 = $array1.Split(',') }
if ($array2.Count -eq 1) { $array2 = $array2.Split(',') }

但是请注意,当您传递包含逗号的单个参数时,这可能会导致问题.

Note, however, that this might cause problems when you're passing a single argument that contains commas.

附录:您为第二次调用描述的行为仅应在逗号前后有空格的情况下发生:

Addendum: the behavior you describe for the second invocation should only occur when you have whitespace before or after a comma:


PS C:\> powershell.exe -F C:\path\to\test.ps1 -array1 one, two -array2 1, 2, 3
array1: one
array2: 1(1)
logName: two

否则,输出应如下所示:

Otherwise the output should be like this:


PS C:\> powershell.exe -F C:\path\to\test.ps1 -array1 one,two -array2 1,2,3
array1: one,two
array2: 1,2,3(1)
logName: log20190723.csv

这篇关于将参数Array传递给powershell.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆