在 Get-ChildItem 中排除连接点
Exclude Junction-Points in Get-ChildItem
我想获取 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"
但是我得到一个
You cannot call a method on a null-valued expression error.
我猜您在 Where-Object
cmdlet 中缺少 scriptblock
的 braces{}
。 -notlike
运算符也使用通配符进行搜索操作。
Get-ChildItem "\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object {$_.Attributes.ToString() -NotLike "*ReparsePoint*"}
根据 Where-Object
cmdlet 的 msdn 文档,您将看到有两种构建 Where-Object 命令的方法。
方法一
Script block.
You can use a script block to specify the property name, a comparison
operator, and a property value. Where-Object returns all objects for
which the script block statement is true.
For example, the following command gets processes in the Normal
priority class, that is, processes where the value of the
PriorityClass property equals Normal.
Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}
方法二
Comparison statement.
You can also write a comparison statement, which is much more like
natural language. Comparison statements were introduced in Windows
PowerShell 3.0.
For example, the following commands also get processes that have a
priority class of Normal. These commands are equivalent and can be
used interchangeably.
Get-Process | Where-Object -Property PriorityClass -eq -Value "Normal"
Get-Process | Where-Object PriorityClass -eq "Normal"
附加信息 -
Starting in Windows PowerShell 3.0, Where-Object adds comparison
operators as parameters in a Where-Object command. Unless specified,
all operators are case-insensitive. Prior to Windows PowerShell 3.0,
the comparison operators in the Windows PowerShell language could be
used only in script blocks.
在您的情况下,您正在将 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*"
我想获取 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"
但是我得到一个
You cannot call a method on a null-valued expression error.
我猜您在 Where-Object
cmdlet 中缺少 scriptblock
的 braces{}
。 -notlike
运算符也使用通配符进行搜索操作。
Get-ChildItem "\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object {$_.Attributes.ToString() -NotLike "*ReparsePoint*"}
根据 Where-Object
cmdlet 的 msdn 文档,您将看到有两种构建 Where-Object 命令的方法。
方法一
Script block.
You can use a script block to specify the property name, a comparison operator, and a property value. Where-Object returns all objects for which the script block statement is true.
For example, the following command gets processes in the Normal priority class, that is, processes where the value of the PriorityClass property equals Normal.
Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}
方法二
Comparison statement.
You can also write a comparison statement, which is much more like natural language. Comparison statements were introduced in Windows PowerShell 3.0.
For example, the following commands also get processes that have a priority class of Normal. These commands are equivalent and can be used interchangeably.
Get-Process | Where-Object -Property PriorityClass -eq -Value "Normal" Get-Process | Where-Object PriorityClass -eq "Normal"
附加信息 -
Starting in Windows PowerShell 3.0, Where-Object adds comparison operators as parameters in a Where-Object command. Unless specified, all operators are case-insensitive. Prior to Windows PowerShell 3.0, the comparison operators in the Windows PowerShell language could be used only in script blocks.
在您的情况下,您正在将 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*"