Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何调整Get ChildItemToDepth以返回数组中的深度?_Arrays_Powershell - Fatal编程技术网

Arrays 如何调整Get ChildItemToDepth以返回数组中的深度?

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;

我使用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
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\