我需要Get-ChildItem返回路径中的所有文件/文件夹。但是,还有指向远程服务器的符号链接。链接是通过以下方式创建的:mklink /D link \\remote-server\folder
如果我运行Get-ChildItem -recurse它,则会跟随到远程服务器的链接,并列出那里的所有文件/文件夹。如果我使用-exclude,它不会列出与排除模式匹配的文件夹,但仍会关闭并列出所有包含的文件和文件夹。
我真正需要的是Get-ChildItem -recurse完全忽略链接,而不要关注它们。
我自己也需要同样的东西。留在...
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-ChildItemNoFollowReparse -Recurse
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
将打印路径的所有子项(包括符号链接/连接),但在递归时不会遵循符号链接/连接。
不需要 的所有功能Get-ChildItem,因此没有将它们写入,但如果您只需要-Recurse和/或-Force,这主要是一个直接替换。
在 Windows 7 和 2012 上的 PSv2 和 PSv4 中进行了(简短)测试。无保修等。
(PS:讨厌 Verb-Noun这些情况......)
| 归档时间: |
|
| 查看次数: |
3542 次 |
| 最近记录: |