[英]Powershell Where-Object -in clause
I am attempting to delete some orphaned folders. 我正在尝试删除一些孤立的文件夹。 I have a list of folder names that are orphaned, and I am attempting to find the corresponding File object.
我有一个孤立的文件夹名称列表,我正在尝试查找相应的File对象。 Unfortuantely, I can't seem to get the
-in
clause to work as I would expect. 不幸的是,我似乎无法使
-in
子句正常工作。
A slightly contrived example: 一个稍微做作的例子:
$orphans = ls | select Name
ls | ?{$_.Name -in $orphans}
ls -Include $orphans
No files are returned. 没有文件返回。
In order for this to work you need to do 为了使其正常工作,您需要做
$orphans = ls | select -ExpandProperty Name
Or inside the Where-Clause
或在
Where-Clause
ls | ?{$_.Name -in $orphans.name}
$orphans
in your code is an object array with a name property. 代码中的
$orphans
是带有name属性的对象数组。 Break that property out into a simple string array and you should get the results you expect. 将该属性分成一个简单的字符串数组,您应该得到期望的结果。
I will also assume that the code sample is just that... a sample. 我还将假设代码示例就是...一个示例。 That code, once working correctly, seems redundant.
该代码一旦正常工作,似乎是多余的。
Powershell v2 Powershell v2
The above code didn't work on Powershell v2. 上面的代码在Powershell v2上不起作用。 A combination of
-ExpandProperty
and -contains
seemed to correct the issue: 的组合
-ExpandProperty
和-contains
似乎纠正问题:
$orphans = ls | select -ExpandProperty Name
ls | ?{$orphans -contains $_.Name}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.