问题描述
我需要 Get-ChildItem
返回路径内的所有文件/文件夹.但是也有指向远程服务器的符号链接.链接由以下方式创建:mklink/D link \\remote-server\folder
I need Get-ChildItem
to return all files/folders inside a path. But there are also symlinks going of to remote servers. The links are created by: mklink /D link \\remote-server\folder
如果我运行 Get-ChildItem -recurse
,它会跟随到远程服务器的链接并从那里列出所有文件/文件夹.如果我使用 -exclude 它不会列出与排除模式匹配的文件夹,但仍然会列出所有包含的文件和文件夹.
If I run Get-ChildItem -recurse
it follows the links to the remote servers and lists all files/folders from there. If I use -exclude it does not list the folder matching the excluded pattern but still goes down and lists all included files and folders.
我真正需要的是 Get-ChildItem -recurse
完全忽略链接而不是跟随它们.
What I actually need is Get-ChildItem -recurse
to ignore links at all and not to follow them.
推荐答案
您可以使用如下命令排除链接:
You can exclude links using a command like below:
gci <your path> -Force -Recurse -ErrorAction 'silentlycontinue' |
Where { !($_.Attributes -match "ReparsePoint") }
看完你的评论:你不能分两步做吗?
After reading your comment: can't you do it in two steps?
$excluded = @(gci <your path> -Force -Recurse -ErrorAction 'silentlycontine' | where { ($_.Attributes -match "ReparsePoint") )
get-childitem -path <your path> -recurse -exclude $excluded
这篇关于如何让 Get-ChildItem 不跟随链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!