0

I have what I'm assuming to be a simple question. I have a large block of code that I have written. In short, it downloads a dynamic list of file names and extensions, imports a csv with the roots of every one of our shares, and compares the files to find matches.

$Directory = Get-ChildItem -Path $path -Recurse -Depth 2 -Include $ExtList -Force

Super simple, right? Except the depth parameter isn't working. It recursively searches through every single level. If I do this:

$Directory = Get-ChildItem -Path $path -Depth 2  -Force

The Depth parameter works perfectly and it only searches through the two levels. If I don't include recurse or depth, it works as expected by searching only the top level. The only difference is that I'm removing the -Include parameter.

$Path is a variable like \server\root\ $ExtList is an array of filenames

Again, they both work individually, but not together.

I need to have both the depth and include parameters in here. Does anybody know what I am doing wrong, or if it's a glitch?

Edit ---------------------------

Doing "Where-Object" I tried this:

$Directory = Get-ChildItem -Path $path -Recurse -Depth 2 -Force | Where-Object {$_.Extension -like $ExtList}

And... nothing happens. For testing, this script takes about 10 minutes to successfully run on a good day, it finished in less than 1 second. (715ms to be exact) When I go into debugging, it's like there is nothing being piped into the where-object.

enter image description here

EDIT------

The $ExtList setup looks like this:

@((Invoke-WebRequest -Uri "https://fsrm.experiant.ca/api/v1/get").content | convertfrom-json | % {$_.filters})

That will get you the exact list and format that I'm using.

$Path pulls from a csv file that looks like this:

enter image description here

This CSV has over 3000 different shares in it. I know it's weird, yes I have to do it this way for our infrastructure.

Nick
  • 1,178
  • 3
  • 24
  • 36
  • 1
    Can you try piping into `Where-Object` instead of using an `-Include` switch? In [several](http://stackoverflow.com/questions/33684178/powershell-5-get-childitem-literalpath-doesnt-work-with-include-anymore) [other](https://technet.microsoft.com/en-us/library/hh849800.aspx) locations, it seems like people are having trouble with some of the `-Depth` switch usage. – gravity Aug 15 '16 at 20:17
  • 2
    As @gravity suggested, pipe to `Where` instead. You shouldn't lose any performance, since the `-Include` argument gets all results from the provider, and then filters down to the specified inclusions. – TheMadTechnician Aug 15 '16 at 20:34
  • So. I'm not as familiar with Where, but when I add in `| Where-Object {$_.Name -eq $ExtList -or $_.Extension -eq $ExtList}`, nothing happens. When I go into the debugger, it's like the variable doesn't get pulled through. @gravity @TheMadTechnician – Nick Aug 16 '16 at 17:00
  • 1
    Don't use `-eq` - [use either](http://stackoverflow.com/questions/36295016/powershell-match-vs-like) `-match` or `-like`. You're comparing strings to each other, and even if they are literally the same (ie: `.ext` and `.ext`), you'll still often see `-eq` fail... (Click the link for a breakdown of the differences between `-match` and `-like`. I prefer `-match` for exact comparisons I know to be static. – gravity Aug 16 '16 at 17:04
  • 1
    @gravity I edited the post to include what I'm doing... maybe you can steer me in the direction of what I'm doing wrong. – Nick Aug 16 '16 at 17:16
  • Since it took a while to clarify, and I 'guessed' at your `$ExtList` and `$path` setup, please further [edit] your answer once more and show those two assignments as well, if my answer doesn't work for you. ;) – gravity Aug 16 '16 at 18:05
  • Your additional edits are not sufficient to continue, unfortunately. Please provide a [mcve], for us, to avoid time being lost further. – gravity Aug 17 '16 at 12:22

1 Answers1

0

After switching to run on a Windows Server 2012 R2 box, the depth parameter works with include. It seems to have been a bug with the version that I was on.

Nick
  • 1,178
  • 3
  • 24
  • 36