powershell - 如何将数组参数传递给 powershell -file?

标签 powershell command-line

我有以下 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/

相关文章:

windows - CMD 脚本 : How to close the CMD

macos - 获取包含要执行的管道的 zsh 别名

Windows 命令行 : START Command Switch/I meaning?

.net - 如何在 C# 中访问 Main 之外的命令行参数

Powershell 3克隆一个对象有意外的结果 - 影响其他对象

arrays - 了解晦涩的 PowerShell 数组语法 : Why does @(11. .20+1) 有效,而 @(1+11..20) 无效?我怎样才能实现后者?

powershell - 将任何文件作为 powershell 脚本执行

c++ - 通过 PATH env 启动程序。变种。有正确的工作目录

string - 如何在 PowerShell 中执行此操作?或 : what language to use for file and string manipulation?

PowerShell 过滤文件大小