我有以下 powershell 脚本:
param(
[Int32[]] $SomeInts = $null,
[String]$User = "SomeUser",
[String]$Password = "SomePassword"
)
New-Object PSObject -Property @{
Integers = $SomeInts;
Login = $User;
Password = $Password;
} | Format-List
如果我执行
.\ParameterTest.ps1 (1..10)
我得到以下信息:Password : SomePassword
Login : SomeUser
Integers : {1, 2, 3, 4...}
但是,如果我在像这样的单独 powershell 实例中运行它,我不会得到预期的结果
powershell -file .\ParameterTest.ps1 (1..10)
.在这种情况下,我得到以下信息:Password : 3
Login : 2
Integers : {1}
我的问题是如何从命令行传递数组或其他复杂数据类型?
最佳答案
数组的各个元素 ( 1..10
) 作为参数传递给脚本。
另一种方法是:
powershell -command {.\test.ps1 (1..10)}
对于同时适用于 powershell 控制台和 cmd 的版本:
powershell -command "&.\test.ps1 (1..10)"
关于powershell - 如何将数组参数传递给 powershell -file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7680679/