我已经注意到PowerShell中的一些我无法解释的行为,但我希望其他人可以.
如果我想从驱动器构建一个文件对象列表C:\,我想忽略快捷方式文件夹(重新分析点)等C:\Documents and Settings\.以下命令运行良好:
$FileList = @(Get-ChildItem -Path C:\ -Recurse -Force -Attributes !ReparsePoint);
$FileList | Where-Object {$_.DirectoryName -like "*Documents and Settings*"};
Run Code Online (Sandbox Code Playgroud)
该Where-Object命令不会按预期返回任何文件,因为C:\Documents and Settings\它是重新分析点.
但是,如果我Test-Connection首先运行该命令,那么该Get-ChildItem命令似乎忽略该-Attributes !ReparsePoint参数,并且它会遍历C:\Documents and Settings\.
Test-Connection -Computer MyComputer;
$FileList = @(Get-ChildItem -Path C:\ -Recurse -Force -Attributes !ReparsePoint);
$FileList | Where-Object {$_.DirectoryName -like "*Documents and Settings*"};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该Where-Object命令显示许多文件.请注意,Test-Connection可以针对任何计算机运行,而不仅仅是本地计算机才能显示此行为.
我在运行PowerShell 4.0和PowerShell 5.1的计算机上重复了这种行为.任何人都可以解释发生了什么?
附加说明:要复制此行为,请确保您使用的是PowerShell的提升实例(以管理员身份运行).如果使用PowerShell的标准实例,则无权查看C:\Documents and Settings\.