我想获取 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
的大括号{}
。此外,-notlike
运算符使用通配符进行搜索操作。
Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object {$_.Attributes.ToString() -NotLike "*ReparsePoint*"}
根据 msdn在 Where-Object
cmdlet 的文档中,您将看到有两种方法可以构建Where-Object 命令。
方法1
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"}
方法2
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
,因此 大括号{}
成为一种必要的邪恶。或者,您可以通过以下方式构造您的 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*"
关于powershell - 排除 Get-ChildItem 中的连接点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250328/