首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排除Get-ChildItem中的连接点

排除Get-ChildItem中的连接点
EN

Stack Overflow用户
提问于 2018-03-13 07:17:32
回答 1查看 2.4K关注 0票数 1

我想得到C:\驱动器中所有用户创建的文件夹的列表。但是,由于连接点的关系,我得到了错误的结果。我试过了

代码语言:javascript
复制
$generalExclude += @("Users", "LocalData", "PerfLogs", "Program Files", "Program Files (x86)", "ProgramData", "sysdat", "Windows", "eplatform", "Intel", "Recovery",  "OneDriveTemp")

Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -ErrorAction 'silentlycontinue' | Where-Object $_.Attributes.ToString() -NotLike "ReparsePoint"

但我得到了一个

不能对空值表达式错误调用方法.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-13 07:48:16

我猜您在您的braces{} cmdlet中缺少了scriptblockWhere-Object。此外,-notlike运算符使用通配符进行搜索操作。

代码语言:javascript
复制
Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object {$_.Attributes.ToString() -NotLike "*ReparsePoint*"}

根据msdn cmdlet的Where-Object文档,您将看到构建Where命令的两种方法。

方法1

脚本块。 可以使用脚本块指定属性名称、比较运算符和属性值。Where-Object返回脚本块语句为true的所有对象。 例如,下面的命令在普通优先级类中获取进程,即PriorityClass属性值等于Normal的进程。 获取-进程\其中-对象{$_.PriorityClass -eq“普通”}

方法2

比较语句。 您还可以编写比较语句,这更像自然语言。在WindowsWindows3.0中引入了比较语句。 例如,下面的命令还获取具有正常优先级类的进程。这些命令是等价的,可以互换使用。 Get-Process _ Where-Object -Property PriorityClass -eq -Value "Normal“Get-Process \ Where-Object PriorityClass -eq "Normal”

补充资料-

从WindowsWHER3.0开始,Where在Where命令中添加比较操作符作为参数.除非指定,否则所有运算符都不区分大小写.在WindowsWindows3.0之前,PowerShell PowerShell语言中的比较运算符只能在脚本块中使用。

在您的例子中,您正在将Where-Object构建为一个scriptblock,因此braces{}成为了一个必要的邪恶。或者,您可以通过以下方式构造Where-Object -

代码语言:javascript
复制
Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object Attributes -NotLike "*ReparsePoint*"

(OR)

代码语言:javascript
复制
Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object -property Attributes -NotLike -value "*ReparsePoint*"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49250328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档