8

正如标题所说,如果我尝试添加-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 Pget-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,因此我们可以为目标驱动器指定凭据)。

有没有人有任何想法?

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

4

1 回答 1

6

更新

  • 从(至少)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失败并显示模糊的错误消息:New-PSDrive : The network resource type is not correct-Persist

# 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

我已经在 GitHub 上报告了这种意外行为,并且已为 PowerShell Core提供了一个修复程序,但目前尚不清楚它将在哪个未来版本中可用(从 PowerShell Core 6.2.0-preview.1 开始编写)。

于 2018-11-12T20:46:00.440 回答