wwwanaya
wwwanaya

Reputation: 137

Searching with Get-ChildItem over the network

I'm having an issue while searching with PowerShell over the network; program get stuck while executing 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

Does anyone know why Get-ChildItem keeps looping and it never stops? I'm using PS v4 no -Depth option available for -Recurse parameter; I'm suspecting that might be the issue.

Upvotes: 0

Views: 405

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

If you want to limit recursion depth in PowerShell v4 and earlier you could wrap Get-ChildItem in a custom function, e.g. like this:

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)
        }
    }
}

Upvotes: 2

Related Questions