EnumerateFiles() on a PSDrive?

(Powershell v4)

I have a script which iterates through a large number of remote machines, mounting a share on each one as a PSdrive. The computer name is used as the name of the drive. Then I’m trying to count the number of files and subdirectories in that location.

Problem 1: Get-ChildItem is sloooow over networks. Especially when there are errors from too long path names, of which there are plenty. Grrr at users.

Problem 2: For reasons that would take a lot of text to explain, I can’t use Powershell Remoting to have the clients themselves count and return the number.

I looked up ways to speed this up, and found out that [System.IO.Directory]::EnumerateFiles() is usually cited as the fastest way to tally up file system objects. I tested it, and it works for what I need to do. So far, so good.

Problem 3: When I put this into my actual script, it doesn’t work. instead, it throws me the following error: “The given path’s format is not supported.”

Further looking into that, it appears to be because the PSDrive is a transient thing, and only Powershell knows about it, but EnumerateFiles() works outside Powershell and has no clue about PSDrives. In order to make this work, I would have to mount a real, persistent OS network drive. Of course, this doesn’t work in the context of my script, where the computer name is used as the name for the PSDrive. Windows doesn’t support drive names longer than a single letter. And it’s not as easy as replacing the variable with a drive letter, because in an effort to speed things up, I’m already launching many instances of the script in parallel.

Can EnumerateFiles() simply not be used on a non-persistent PSDrive, period? Or is there a way to make this work that doesn’t involve mapping persistent network drives?

P.S.: Please don’t skin me alive for crimes against programming. =P I’m aware that the question “why would you even do such a thing” might be on the tips of your tongues. The answer is simply: someone told me to produce this data. And as I’m not a trained developer, dirty scripting is all I know.

submitted by /u/Streetwind
[link] [comments]

Leave a Reply