When mapping to network drive I get an error when retrieving files(not always)


I am mapping a network drive in Powershell, but have some weird behavior when retrieving files from the drive. Sometimes it works most of the time it doesn’t. I have two locations, one lets say from GIT and the other one is my network drive I am creating. I want to compare the files from both locations and see if there is a difference between them. Let me explain what I am doing in the code:

$somePath = "\\testtest.lan\Res\Application\TestApp\SomeFiles"

$PsDrive = New-PSDrive -Name "M" -PSProvider "FileSystem" -Root $somePath

I also tried : $PsDrive = New-PSDrive -Name "M" -PSProvider "FileSystem" -Root $somePath -Persist -Scope Global

Next, I am trying to get all the files from my network drive and here the problem happens:

$someVariable= Get-ChildItem "M:\" -Recurse | Select-Object -Property *, @{n='Path';e={$_.FullName -replace [regex]::Escape("M:\")}}

Here I am facing some strange behavior, sometimes it returns all the child items as it should, but most of the time I get an error saying: “Get-ChildItem : An unexpected network error occurred.” or couple of times it happened to give me this error and nothing more :

At line:1 char:1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (M:\:String) [Get-ChildItem], IOException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

When I do Get-PsDrive I can see the mapped drive there.

Do you guys think I am doing something wrong in the code or some problem with the network location I am trying to access. It shouldn’t be related to permissions since there are times when it’s working correctly. Is it possible to see a more detailed or accurate error message?

Last but not least, sorry If I didn’t followed some forum rules, I am new here. Will try to improve in my next post. :slight_smile:



That appears to just be a generic network error, so it could be many things. As it’s sporadic, it’s even more of a pain to troubleshoot root cause. Maybe create a loop to try to get files 3 times to attempt to get the files with a 15 sec sleep or something. Personally have spent many many hours trying to root cause something like this, but scripting around it is a bandaid. You’d have to look at all of touchpoints, DFS, NFS, Windows, etc. to try get logs locally on your client and touchpoints.