1

我想获取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"

但我得到一个

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

4

1 回答 1

3

我猜您在cmdlet中缺少braces{}for your 。运算符也使用通配符进行搜索操作。scriptblockWhere-Object-notlike

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

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

方法一

脚本块

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

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

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

方法二

比较声明

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

例如,以下命令还获取优先级为 Normal 的进程。这些命令是等效的,可以互换使用。

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-Objectas ascriptblock并因此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*"
于 2018-03-13T07:48:16.023 回答