通过网络搜索Get-ChildItem

时间:2018-01-16 18:16:10

标签: powershell scripting powershell-v4.0

我在通过网络搜索PowerShell时遇到问题;程序在执行Get-ChildItem时卡住了。

# creating search string
$date = "*2018-01-10*"
$format = ".avi"
$toSearch = $date + $format
echo $toSearch

# Verifying A: drive to be disconnected
net use /d A: /y

# connecting Network drive to local drive A:
if (Test-Connection -ComputerName PROC-033-SV -Quiet) {net use A: \\PROC-033-SV\c$}

# getting list of directories to search on
$userList = Get-ChildItem -Path A:\users\*.* -Directory

# verifying list of directories prior to search
echo $userList

# searching through Network, on A:\Users\ subdirectories for $toSearch variable
Get-ChildItem -Path $userList -Include $toSearch -Recurse -Force

# *** HERE's where the program get stuck, it nevers stop searching
# it nevers reach pause

pause

PSError Get-ChildItem

有谁知道为什么Get-ChildItem会保持循环并且它永远不会停止? 我在-Depth参数中使用PS v4 no -Recurse选项;我怀疑这可能是个问题。

1 个答案:

答案 0 :(得分:2)

如果要在PowerShell v4及更早版本中限制递归深度,可以将Get-ChildItem包装在自定义函数中,例如像这样:

function Get-ChildItemRecursive {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$false,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]
        [string[]]$Path = $PWD.Path,

        [Paramter(Mandatory=$false)]
        [string]$Filter = '*.*',

        [Parameter(Mandatory=$false)]
        [int]$Depth = 0
    )

    Process {
        Get-ChildItem -Path $Path -Filter $Filter
        if ($Depth -gt 0) {
            Get-ChildItem -Path $Path |
                Where-Object { $_.PSIsContainer } |
                Get-ChildItemRecursive -Filter $Filter -Depth ($Depth - 1)
        }
    }
}