将参数作为数组传递给 PowerShell 函数 [英] Passing arguments as array to PowerShell function

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

问题描述

我想弄清楚如何将多个字符串作为数组传递给 powershell 函数.

I'm trying to figure out how I can pass multiple strings as an array to a powershell function.

function ArrayCount([string[]] $args) {
    Write-Host $args.Count
}

ArrayCount "1" "2" "3"
ArrayCount "1","2","3"
ArrayCount @("1","2","3")

印刷品

2
0
0

如何将具有 3 个值的数组传递给 ArrayCount 函数?为什么某些调用的计数为零?

How can I pass an array with 3 values to the ArrayCount function? Why is the Count zero for some of the invocations?

推荐答案

在 PowerShell 中,$args 是一个自动变量,指的是未命名 论据.只需更改您的参数名称:

In PowerShell, $args is a automatic variable that refers to unnamed arguments. Just change your parameter name:

function ArrayCount([string[]] $myParam) {
    Write-Host $myParam.Count
}

您将获得预期的输出:

1
3
3

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

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