【发布时间】:2018-12-17 22:12:47
【问题描述】:
我很困惑我在 List 的 ForEach 方法语法中做错了什么?
PS D:\ntt> $nicInfo.IpConfigurations.Count
2
PS D:\ntt> $nicInfo.IpConfigurations[0]
PrivateIpAddressVersion Name Primary PrivateIpAddress PrivateIpAllocationMethod Subnet Name PublicIpAddress Name ProvisioningState
----------------------- ---- ------- ---------------- ------------------------- ----------- -------------------- -----------------
IPv4 ipconfig1 True 10.233.0.4 Dynamic Succeeded
PS D:\ntt> $nicInfo.IpConfigurations.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True List`1 System.Object
PS D:\ntt> $nicInfo.IpConfigurations.ForEach({$_})
PS D:\ntt>
【问题讨论】:
-
你是如何创建对象的?你确定这个方法存在吗?使用standard foreach loop 怎么样?
-
这看起来像一个错误! [grin]
.Where方法有效。当集合类型是 Generic.List 时,$_和$PSItem似乎都不适用于.ForEach()。多么有趣......使用Write-Host将输出发送到主机,而不是$_的东西。使用Write-Output根本没有显示任何内容。看起来输出流的处理方式有问题。 -
您期望发生什么?
ForEach因其副作用而被执行,而您似乎没有任何副作用。$nicInfo.IpConfigurations.ForEach({param($i) write-host "$i"})是做什么的? -
我想在foreach语句中做一些数据操作,例子就是例子。
-
@GregorySuvalian
List<T>有自己的ForEach方法。因此,它优于ForEachPowerShell 添加到集合中。
标签: azure powershell azure-powershell