在 Powershell 中,如何传递 IEnumerable 参数?

Posted

技术标签:

【中文标题】在 Powershell 中,如何传递 IEnumerable 参数?【英文标题】:From within Powershell, how do I pass an IEnumerable argument? 【发布时间】:2013-03-06 19:00:26 【问题描述】:

我正在尝试使用 Microsoft.Lync.Model.Contact.GetContactInformation() 方法。有两种版本,一种采用普通枚举,另一种采用具有多个值的 IEnumerable。

http://msdn.microsoft.com/en-us/library/lync/hh347568.aspx

我可以半成功地使用第一个版本(直到我尝试检索一个没有价值的版本),但我无法理解我应该如何传递多个参数。

$contactInfo = $c[$c.Count - 1].Participants[1].Contact.GetContactInformation([int[]]("FirstName", "LastName", "DisplayName", "PrimaryEmailAddress"))

(以上行不通。)

有人可以将我推向正确的方向吗?

【问题讨论】:

为什么要将字符串数组转换为 int 数组? 我怎么知道,我宁愿写一个 bash 脚本。实际上,我宁愿微软编写可适度扩展的软件,这样我就不必为了得到 Growl 通知而陷入这种废话。 【参考方案1】:

最简单的语法将接近您现在所拥有的。该方法采用ContactInformationType 值的枚举,因此您的强制转换应该是这些值的数组,而不是int

另外,您可以使用$foobar[-1] 作为$foo[$foo.Count - 1] 的糖,即获取最后一个元素。

$contactInfo = $c[-1].Participants[1].Contact.GetContactInformation([Microsoft.Lync.Model.ContactInformationType[]] @("FirstName", "LastName", "DisplayName", "PrimaryEmailAddress"))

【讨论】:

【参考方案2】:

我认为如果您尝试以下方法,它会起作用:

$contactInfo = $c[$c.Count - 1].Participants[1].Contact.GetContactInformation(
                [int[]](
                        [int]Microsoft.Lync.Model::ContactInformationType.FirstName,
                        [int]Microsoft.Lync.Model::ContactInformationType.LastName,
                        [int]Microsoft.Lync.Model::ContactInformationType.DisplayName,
                        [int]Microsoft.Lync.Model::ContactInformationType.PrimaryEmailAddress))

【讨论】:

【参考方案3】:

该方法本身可能不知道“FirstName”是什么。尝试创建一个枚举值数组,例如:

$information = [Microsoft.Lync.Model.ContactInformationType]::FirstName, [Microsoft.Lync.Model.ContactInformationType]::LastName, [Microsoft.Lync.Model.ContactInformationType]::DisplayName, [Microsoft.Lync.Model.ContactInformationType]::PrimaryEmailAddress
$contactInfo = $c[$c.Count - 1].Participants[1].Contact.GetContactInformation($information)

【讨论】:

以上是关于在 Powershell 中,如何传递 IEnumerable 参数?的主要内容,如果未能解决你的问题,请参考以下文章

如何从PowerShell命令行转义管道字符以传递到非PowerShell命令

如何使用 powershell routes.jason 传递正文

如何在不连接 Powershell 的情况下传递参数? [复制]

我想请教一下,powershell的脚本之间如何传递全局变量。

使用 Powershell 中的 Stack,如何传递包含空格的测试参数?

如何在 RegEx 替换中将 RegEx 令牌传递给 PowerShell 子表达式?