如何将数组参数传递给 powershell -file?
我有以下 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}
我的问题是如何从命令行传递数组或其他复杂数据类型?
I have the following powershell script:
param(
[Int32[]] $SomeInts = $null,
[String]$User = "SomeUser",
[String]$Password = "SomePassword"
)
New-Object PSObject -Property @{
Integers = $SomeInts;
Login = $User;
Password = $Password;
} | Format-List
If I execute .\ParameterTest.ps1 (1..10)
I get the following:
Password : SomePassword
Login : SomeUser
Integers : {1, 2, 3, 4...}
However, I don't get the expected results if I run it in a separate powershell instance like this powershell -file .\ParameterTest.ps1 (1..10)
. In that case I get the following:
Password : 3
Login : 2
Integers : {1}
My question is how can I pass the array, or other complex data type from a command line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数组的各个元素 (
1..10
) 作为参数传递给脚本。另一种方法是:
对于同时可以从 powershell 控制台和 cmd 运行的版本:
The individual elements of the array (
1..10
) are being passed as parameters to the script.An alternative would be to do:
For a version that works both from powershell console and cmd:
答案是使用 powershell.exe -EncodedCommand 并对参数进行 Base64 编码。对此的说明位于 PowerShell.exe 控制台帮助 technet 页面。我已经将他们的仪式压缩成一句话:
The answer is to use
powershell.exe -EncodedCommand
and to base64 encode the parameters. The description for this is on the PowerShell.exe Console Help technet page. I have compressed their version of the ceremony to do this into a one-liner: