在 Get-ChildItem 中排除连接点

问题描述 投票:0回答:2

我想获取

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

$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"

但是我得到了

您无法在空值表达式错误上调用方法。

powershell get-childitem junction
2个回答
3
投票

我猜您在

braces{}
cmdlet 中缺少
scriptblock
Where-Object
-notlike
运算符也使用通配符进行搜索操作。

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

根据 Where-Object cmdlet 的

msdn
文档,您将看到有两种方法可以构建Where-Object 命令。

方法1

脚本块

可以使用脚本块来指定属性名称、比较 运算符和属性值。 Where-Object 返回所有对象 脚本块语句是正确的。

例如,以下命令获取Normal中的进程 优先级,即进程的值 PriorityClass 属性等于 Normal。

Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}

方法2

比较声明.

你也可以写一个比较语句,这更像是 自然语言。 Windows 中引入了比较语句 PowerShell 3.0.

例如,以下命令还可以获取具有 优先级为普通。这些命令是等效的并且可以 可以互换使用。

Get-Process | Where-Object -Property PriorityClass -eq -Value "Normal"

Get-Process | Where-Object PriorityClass -eq "Normal"

附加信息 -

从 Windows PowerShell 3.0 开始,Where-Object 添加了比较 运算符作为Where-Object 命令中的参数。除非另有说明, 所有运算符都不区分大小写。在 Windows PowerShell 3.0 之前, Windows PowerShell 语言中的比较运算符可以是 仅在脚本块中使用。

就您而言,您正在将

Where-Object
构建为
scriptblock
,因此
braces{}
成为必要之恶。或者,您可以通过以下方式构建您的
Where-Object
-

Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object Attributes -NotLike "*ReparsePoint*"

(或)

Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object -property Attributes -NotLike -value "*ReparsePoint*"

0
投票

获取子项“\localhos

© www.soinside.com 2019 - 2024. All rights reserved.