Powershell 如何使Get ChildItem不跟随链接
我需要Powershell 如何使Get ChildItem不跟随链接,powershell,Powershell,我需要Get ChildItem返回路径中的所有文件/文件夹。但也有符号链接指向远程服务器。链接由以下文件夹创建:mklink/D link\\remote server\folder 如果我运行getchilditem-recurse,它将跟踪到远程服务器的链接,并列出其中的所有文件/文件夹。如果使用-exclude,它不会列出与排除模式匹配的文件夹,但仍会向下列出所有包含的文件和文件夹 我真正需要的是getchilditem-recurse忽略链接,而不是跟随链接。您可以使用如下命令排除链接
Get ChildItem
返回路径中的所有文件/文件夹。但也有符号链接指向远程服务器。链接由以下文件夹创建:mklink/D link\\remote server\folder
如果我运行getchilditem-recurse
,它将跟踪到远程服务器的链接,并列出其中的所有文件/文件夹。如果使用-exclude,它不会列出与排除模式匹配的文件夹,但仍会向下列出所有包含的文件和文件夹
我真正需要的是
getchilditem-recurse
忽略链接,而不是跟随链接。您可以使用如下命令排除链接:
gci <your path> -Force -Recurse -ErrorAction 'silentlycontinue' |
Where { !($_.Attributes -match "ReparsePoint") }
gci-Force-Recurse-ErrorAction'silentlycontinue'|
其中{!($\.Attributes-match“repassepoint”)}
看完你的评论后:你不能分两步做吗
$excluded = @(gci <your path> -Force -Recurse -ErrorAction 'silentlycontine' | where { ($_.Attributes -match "ReparsePoint") )
get-childitem -path <your path> -recurse -exclude $excluded
$excluded=@(gci-Force-Recurse-ErrorAction'silentlycontine'|其中{($.Attributes-match“ReparsePoint”))
get childitem-path-recurse-exclude$excluded
我自己也需要同样的东西。决定
Function Get-ChildItemNoFollowReparse
{
[Cmdletbinding(DefaultParameterSetName = 'Path')]
Param(
[Parameter(Mandatory=$true, ParameterSetName = 'Path', Position = 0,
ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $Path
,
[Parameter(Mandatory=$true, ParameterSetName = 'LiteralPath', Position = 0,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias('PSPath')]
[String[]] $LiteralPath
,
[Parameter(ParameterSetName = 'Path')]
[Parameter(ParameterSetName = 'LiteralPath')]
[Switch] $Recurse
,
[Parameter(ParameterSetName = 'Path')]
[Parameter(ParameterSetName = 'LiteralPath')]
[Switch] $Force
)
Begin {
[IO.FileAttributes] $private:lattr = 'ReparsePoint'
[IO.FileAttributes] $private:dattr = 'Directory'
}
Process {
$private:rpaths = switch ($PSCmdlet.ParameterSetName) {
'Path' { Resolve-Path -Path $Path }
'LiteralPath' { Resolve-Path -LiteralPath $LiteralPath }
}
$rpaths | Select-Object -ExpandProperty Path `
| ForEach-Object {
Get-ChildItem -LiteralPath $_ -Force:$Force
if ($Recurse -and (($_.Attributes -band $lattr) -ne $lattr)) {
Get-ChildItem -LiteralPath $_ -Force:$Force `
| Where-Object { (($_.Attributes -band $dattr) -eq $dattr) -and `
(($_.Attributes -band $lattr) -ne $lattr) } `
| Get-ChildItemNoFollow -Recurse
}
}
}
}
将打印路径的所有子项(包括符号链接/连接),但在递归时不会跟随符号链接/连接
不需要Get ChildItem
的所有功能,因此没有将它们写入,但如果您只需要-Recurse
和/或-Force
,这基本上是一个替代品
在Windows 7和2012上的PSv2和PSv4中进行了测试(简要)。无保修等
(PS:Hate动词名词在这些情况下…你实际上可以做:
get-childitem -recurse -attributes !reparsepoint
好的,这将排除链接本身,但不会排除在Powershell 5.1中通过-recurse获得的链接下面的文件。这两种方法都不会
| where attributes -notmatch reparsepoint
实际上,它在powershell 6中的工作与预期的一样。与-exclude类似的结果。它没有列出“重解析点”它本身,但它的所有内容。它仍然遵循符号链接。原因将是-exclude和where在列表生成后应用。gci获取所有对象,然后其管道将结果传输到where,其中一个排除了一些。检索对象时似乎只应用了-filter。检查此链接:很遗憾,这不起作用。th您第二个建议的第一个命令已经在链接的网络树中遍历,第二个命令也会这样做,然后不仅列出ReparsePoint项,而且列出ReparsePoint下的每个项,它是一个简单的文件或目录。-排除不起作用,即使我排除了链接的名称。我需要一个负-筛选器应用于b在powershell<3中进行扫描之前,唯一的方法似乎是自己执行递归函数。这里,超级用户powershell 3中的解决方案似乎可以通过以下方式完成:Get ChildItem-recurse-Attributes!ReparsePoint查看有关stackoverflow的文章:-Attributes检查似乎只适用于叶子,而不是文件夹树。因此,它愉快地遍历重分析点,然后返回它找到的任何内容,因为另一端的文件不是重分析点。流的优雅使用。通过PS 5.1测试,此解决方案确实排除了重分析点的导航。