我想得到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"但我得到了一个
不能对空值表达式错误调用方法.
发布于 2018-03-13 07:48:16
我猜您在您的braces{} cmdlet中缺少了scriptblock的Where-Object。此外,-notlike运算符使用通配符进行搜索操作。
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 -
Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object Attributes -NotLike "*ReparsePoint*"(OR)
Get-ChildItem "\\localhost\c$" -Directory -force -Exclude $generalExclude -erroraction 'silentlycontinue' | Where-Object -property Attributes -NotLike -value "*ReparsePoint*"https://stackoverflow.com/questions/49250328
复制相似问题