Arrays 如何调整Get ChildItemToDepth以返回数组中的深度?
我使用PowerShell 2.0并使用一个函数列出2级深度上以“\S”结尾的所有目录 示例中,Push Location=“\\MyServer\Shared\toto\”结果为:Arrays 如何调整Get ChildItemToDepth以返回数组中的深度?,arrays,powershell,Arrays,Powershell,我使用PowerShell 2.0并使用一个函数列出2级深度上以“\S”结尾的所有目录 示例中,Push Location=“\\MyServer\Shared\toto\”结果为: \\MyServer\Shared\toto\Folder1_S \\MyServer\Shared\toto\Folder1_S\Folder2_S \\MyServer\Shared\toto\Folder1_S\Folder3_S 现在,我想让这个函数返回数组中的级别深度数 例如,我想要这个结果 1;
\\MyServer\Shared\toto\Folder1_S
\\MyServer\Shared\toto\Folder1_S\Folder2_S
\\MyServer\Shared\toto\Folder1_S\Folder3_S
现在,我想让这个函数返回数组中的级别深度数 例如,我想要这个结果
1; \\MyServer\Shared\toto\Folder1_S
2;\\MyServer\Shared\toto\Folder1_S\Folder2_S
2; \\MyServer\Shared\toto\Folder1_S\Folder3_S
如何使用函数返回具有深度的数组?不要输出
FullName
-值,而是使用当前深度和FullName创建一个字符串。Ex(我也清理了一下):
function Get-ChildItemToDepth {
param(
[String]$Path = $PWD,
[String]$Filter = "*_S",
[Byte]$ToDepth = 2,
[Byte]$CurrentDepth = 0,
[Switch]$DebugMode
)
$CurrentDepth++
if ($DebugMode) { $DebugPreference = "Continue" }
Get-ChildItem $Path | ForEach-Object {$_ | Where-Object { ($_.Attributes -match "Directory") -and ($_.Name -like $Filter) } | Select-Object -ExpandProperty FullName
#Write-Host $CurrentDepth
if ($_.PsIsContainer) {
if ($CurrentDepth -le $ToDepth) {
# Callback to this function
Get-ChildItemToDepth -Path $_.FullName -Filter $Filter -ToDepth $ToDepth -CurrentDepth $CurrentDepth
} else {
Write-Host $("Skipping GCI for Folder: $($_.FullName) " +
"(Why: Current depth $CurrentDepth vs limit depth $ToDepth)")
}
}
}
}
function Get-ChildItemToDepth {
param(
[String]$Path = $PWD,
[String]$Filter = "*_S",
[int]$MaxDepth = 2,
[int]$CurrentDepth = 1,
[Switch]$DebugMode
)
if ($DebugMode) { $DebugPreference = "Continue" }
Get-ChildItem $Path | Where-Object { $_.PSIsContainer } | ForEach-Object {
#Write-Host $CurrentDepth
if ($_.Name -like $Filter) {
#Match found. Output "Level; Path"
"$CurrentDepth; $($_.FullName)"
}
#Recursion
if ($CurrentDepth -lt $MaxDepth) {
# Callback to this function
Get-ChildItemToDepth -Path $_.FullName -Filter $Filter -MaxDepth $MaxDepth -CurrentDepth ($CurrentDepth + 1)
} else {
Write-Host $("Skipping GCI for Folder: $($_.FullName) " +
"(Why: Current depth $CurrentDepth vs limit depth $MaxDepth)")
}
}
}
Get-ChildItemToDepth -Path \\MyServer\Shared\toto\