【问题标题】:PowerShell 'in' operator and Object[]PowerShell 'in' 运算符和 Object[]
【发布时间】:2015-04-16 09:32:38
【问题描述】:

我喜欢使用 PowerShell 查找驻留在 iSCSI 磁盘上的所有磁盘分区。

要查找我们可以使用的所有 iSCSI 磁盘对象 ID:

Get-Disk | Where BusType -ieq iscsi | Select -prop ObjectId

使用in 运算符,这应该返回所有分区:

Get-Partition | Where DiskId in (Get-Disk | Where BusType -ieq iscsi | Select -prop ObjectId)

不幸的是,这个命令返回一个典型的 PowerShell 错误:

Where-Object : A positional parameter cannot be found that accepts argument 'System.Object[]'.
At line:1 char:17
+ Get-Partition | Where DiskId in (Get-Disk | Where BusType -ieq iscsi | Select -p ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand

如何正确使用in 运算符?

【问题讨论】:

  • 注意运算符是-in,而不是in

标签: powershell in-operator


【解决方案1】:

select -prop ObjectId 不会按照你的想法去做。它会创建一个具有属性子集的新对象。

请改用select -exp ObjectId。它返回属性值本身,而不将它们包装在自定义对象中。

Get-Partition | Where DiskId -in (Get-Disk | Where BusType -ieq iscsi | Select -exp ObjectId)

【讨论】:

  • 太酷了!非常感谢您的解释和解决方案!
猜你喜欢
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
相关资源
最近更新 更多