首页 > 解决方案 > 从映射的网络驱动器检索文件时出错

问题描述

我在 PowerShell 中创建了一个映射的网络驱动器,但是在从驱动器中检索文件时有一些奇怪的行为。当我创建驱动器(不带-Persist -Scope Global)时:

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

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

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

在我第一次尝试Get-Child如上所述做时,我收到一条错误消息:

“Get-ChildItem:长度不能小于零”

在我第二次尝试时,它工作得很好,我可以看到列出的所有文件。下次我尝试它会停止工作(在同一会话中)并出现不同的错误:

Get-ChildItem:发生意外的网络错误。
在 line:1 char:1
+ Get-ChildItem "M:\" -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (\testtest.l..er\Res\Application\TestApp\SomeFiles:String) [Get-ChildItem], IOException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

当我尝试像这样创建映射的网络驱动器时:

New-PSDrive -Name "M" -PSProvider "FileSystem" -Root $somePath -Persist -Scope Global

它会立即工作几次,但它会在一段时间后停止工作,并出现与上述相同的错误:

Get-ChildItem:发生意外的网络错误。
在 line:1 char:1
+ Get-ChildItem "M:\" -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (\testtest.l..er\Res\Application\TestApp\SomeFiles:String) [Get-ChildItem], IOException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

当我这样做时,Get-PsDrive我可以在那里看到映射的驱动器。

我是在代码中做错了什么,还是我尝试访问的网络位置有问题。它不应该与权限相关,因为有时它可以正常工作,或者可能是。当我从文件资源管理器尝试时,我能够访问网络位置。

让我知道你对此的看法。

谢谢,

安德烈

标签: powershellnetwork-drive

解决方案


推荐阅读