简体   繁体   English

在PowerShell中将数组作为参数传递

[英]Passing an array as a parameter in PowerShell

I'm having some trouble with Powershell, I have code that looks like this: 我在使用Powershell时遇到了一些麻烦,我的代码如下所示:

param
(
    [Parameter(Mandatory=$true)]
    $Array = ''
)

foreach ($member in $Array){
    Write-Host "TEST"

    $server = $member["server"]
    $path = $member["path"]
    $key = $member["key"]

    Write-Host $server
    Write-Host $path
    Write-Host $key
}

When I provide the following line as input, the script only prints "TEST" and nothing further, but when I define $Array within the script itself with the exact same code, the script works as expected. 当我提供以下行作为输入时,脚本仅显示“ TEST”,而没有其他内容,但是当我在脚本本身中使用完全相同的代码定义$Array时,脚本将按预期工作。

@(@{"server" = "server1"; "path" = "\\path1"; "key" = "key1"}, @{"server" = "server2"; "path" = "\\path2"; "key" = "key2"}, @{"server" = "server3"; "path" = "\\path3"; "key" = "key3"})

The expected output is: 预期的输出是:

TEST
server1
\\path1
key1
TEST
server2
\\path2
key2
TEST
server3
\\path3
key3

This is my first reach into PowerShell coming from a Python background. 这是我从Python背景第一次接触PowerShell。

It seems that if this array is passed at the command line level, it works. 看来,如果此数组在命令行级别传递,则可以正常工作。 However, if the script prompts for the input and it's entered at that point, it fails. 但是,如果脚本提示输入并在此时输入,则它将失败。 Where am I going wrong? 我要去哪里错了?

To clarify, the below code works perfectly well, with each key's value being printed to the console. 需要说明的是,以下代码可以很好地工作,每个键的值都打印到控制台上。

$Array = @(@{"server" = "server1"; "path" = "\\path1"; "key" = "key1"}, @{"server" = "server2"; "path" = "\\path2"; "key" = "key2"}, @{"server" = "server3"; "path" = "\\path3"; "key" = "key3"})

foreach ($member in $Array){
    Write-Host "TEST"

    $server = $member["server"]
    $path = $member["path"]
    $key = $member["key"]

    Write-Host $server
    Write-Host $path
    Write-Host $key
}

I think this boils down to when you are being prompted for the input (by not specifying the value for the parameter on the command line) it is being read in a similar manner to using the Read-Host cmdlet. 我认为这可以归结为当系统提示您输入输入时(通过在命令行上未指定参数的值),其读取方式与使用Read-Host cmdlet的方式类似。 The output of that cmdlet is either a [System.String] or [System.Security.SecureString] . 该cmdlet的输出是[System.String][System.Security.SecureString]

As such, instead of the input to the script/function being a proper object, it is just a long string. 这样,它不是一个适当对象的脚本/函数输入,而是一个长字符串。 You can see the difference if you add a debug line to your script just after the param() block: 如果在param()块之后向脚本添加调试行,则可以看到区别:

Write-Debug ($Array | Out-String)

You will need to set $DebugPreference="Continue" on the console, before you run the script, to view debug output. 在运行脚本之前,需要在控制台上设置$DebugPreference="Continue" ,以查看调试输出。 When you run the script, you should see the difference it makes using the prompted input. 运行脚本时,应该看到使用提示输入的区别。

Personally I wouldn't try much harder to collect "complex" data from the console prompt and look at ways to make sure you feed in the data on the command line, as you have already tried. 就个人而言,我将不费劲地从控制台提示符处收集“复杂”数据,并尝试确保已在命令行中输入数据的方法,就像您已经尝试过的那样。

You need to pass a parameter like this: 您需要传递这样的参数:

.\1.ps1 @(@{"server" = "server1"; "path" = "\\path1"; "key" = "key1"}, @{"server" = "server2"; "path" = "\\path2"; "key" = "key2"}, @{"server" = "server3"; "path" = "\\path3"; "key" = "key3"})

When you use ISE, the parameter is handled as a string, because you actually pass a string and PowerShell syntax is not applied to it. 使用ISE时,该参数将作为字符串处理,因为您实际上传递了一个字符串,并且PowerShell语法未应用于该字符串。 You can verify it by adding $Array.GetType().Name to your script. 您可以通过在脚本中添加$Array.GetType().Name来进行验证。

It somewhat helps if you will define your variable properly: 如果您正确定义变量,会有所帮助:

[System.Collections.Generic.Dictionary``2[System.String,System.String][]] Array

But UI is really designed to pass strings or string arrays and not more complex structures. 但是,UI实际上是设计用来传递字符串或字符串数​​组而不是更复杂的结构的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM