【问题标题】:Cannot add "-persist" to New-PSDrive - getting error "network resource type is not correct"无法将“-persist”添加到 New-PSDrive - 出现错误“网络资源类型不正确”
【发布时间】:2019-04-15 11:46:25
【问题描述】:

正如标题所说,如果我尝试将-persist 添加到 cmdlt,它会返回错误:

New-PSDrive : The network resource type is not correct At line:1 char:1 + New-PSDrive -Name P -Root <redacted> ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (P:PSDriveInfo) [New-PSDrive], Win32Exception + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand

作为参考,这是 PowerShell 中的管理员会话,但如果我在常规用户会话中执行此操作,我会得到完全相同的响应:

PS> $PSVersionTable.psversion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      16299  666

从一开始:我确认我要使用的驱动器号可用(在本例中为 P)

PS> get-psdrive

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
Alias                                  Alias
C                  36.80         22.11 FileSystem    C:\                                               windows\system32
Cert                                   Certificate   \
D                                      FileSystem    D:\
Env                                    Environment
Function                               Function
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE                  ...ntrol\NetworkProvider\Order
Variable                               Variable
WSMan                                  WSMan

然后我尝试运行:(我已经将$locale 定义为变量,例如'\\foo\bar\')。

New-PSDrive -PSProvider FileSystem -Name P -Root $locale -Scope global

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
P                                      FileSystem    \\foo\bar\

快乐的日子。这样可行。但当然,它对 Windows 资源管理器是不可见的,因为它只是在这个 PS 会话中。所以,我销毁它(remove-psdrive P)并验证它已经消失(get-psdrive),这里没有显示,然后尝试创建一个持久版本(相同的命令,只是使用-persist):

PS> New-PSDrive -PSProvider FileSystem -Name P -Root $locale -Scope global -Persist
New-PSDrive : The network resource type is not correct
At line:1 char:1
+ New-PSDrive -PSProvider FileSystem -Name P -Root $locale -Scope globa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (P:PSDriveInfo) [New-PSDrive], Win32Exception
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand

它失败了。对于上下文,我这样做是为了可以使用带有远程 PSDrive 的 RoboCopy 编写脚本(我们想要使用 robocopy,因为它内置了报告、属性管理以及在目标位置维护文件结构的能力,我们想要使用PSDrives,因此我们可以为目标驱动器指定凭据)。

有人有什么想法吗?

编辑: 哦,伙计……我不敢相信……但是以防万一其他人发现它,我将离开它。问题是我路径中的尾随\。我删除了它,它仍然很好。

【问题讨论】:

    标签: powershell windows-scripting drive-mapping powershell-v5.1 new-psdrive


    【解决方案1】:

    更新

    • 这个问题已从(至少)PowerShell 7.0.5 开始修复 - 但是,它在 Windows PowerShell (v5.1) 中仍然存在,并且不太可能得到固定在那里。

    • 一个相关问题仍然存在于 v7.1(撰写本文时的最新稳定版本)是使用 -Persist 也会导致误导性错误消息在真正的错误情况下:谢谢,dgm

      • 如果目标 主机 碰巧无法访问/不支持 CIFS,您还会收到 The network resource type is not correct 错误。
      • 如果主机可以访问,但目标目录不存在,你会得到The specified network resource or device is no longer available.
      • 相比之下,如果您使用-Persist,则错误消息更合理:The specified drive root "\\foo\bar" either does not exist, or it is not a folder.(尽管它没有指出两个错误条件中的哪一个适用)。李>

    [仅限 Windows PowerShell 和 PowerShell Core 6.x] 如您所见:

    不要指定带有尾随 \ 的驱动器根路径,因为这样做会导致 New-PSDrive 在使用 -Persist 时出现模糊错误消息 New-PSDrive : The network resource type is not correct 失败:

    # Trailing '\': only OK *without* -Persist.
    New-PSDrive -root \\foo\bar\ -name N  -PSProvider filesystem
    
    # !! Breaks with -Persist:
    New-PSDrive -root \\foo\bar\ -name N  -PSProvider filesystem
    
    # No trailing '\': OK both with and without -Persist
    New-PSDrive -root \\foo\bar -name N  -PSProvider filesystem
    

    我已报告此意外行为 on GitHubfix has been green-lighted for PowerShell Core,但尚不清楚它会在哪个未来版本中可用(从 PowerShell Core 6.2.0-preview.1 开始编写)。

    【讨论】:

    • 谢谢,@dgm。我已将您的评论折叠成答案顶部的注释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 2017-07-31
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多