在PowerShell中挂载永久驱动器

3

如何在PowerShell中永久挂载驱动器?即使系统重启后也能保持挂载状态?

当我使用New-PSDrive -Persist命令(当然还需要其他必要参数)时,它只在当前的Windows会话中有效。我使用的是PS 5.1。

我可以在文件资源管理器中看到该驱动器,它在系统中是持久的,但我无法从PowerShell ISE中看到它,即使我以管理员身份运行。我必须使用New-PSDrive才能从网络驱动器加载脚本,以便进行工作。每次重新启动计算机时,该驱动器再次无法从ISE中看到。这种情况很烦人,因为我需要一遍又一遍地重复操作。


"net use" 将会起作用。 - 4c74356b41
1
@4c74356b41 New-PSDrive 是 PowerShell 中等同于 net use 的命令。 - Maximilian Burszley
你是以不同的用户运行ISE吗?你能从Get-PSDrive中看到驱动器吗? - Maximilian Burszley
不,我以提升的方式运行ISE,但是在与我登录到系统的相同用户下。并且我无法通过“Get-PSDrive”看到这个驱动器。 - Petr Synek
2个回答

1

对我来说,命令行

New-PSDrive -Name "X" -Root "\\some\path" -PSProvider "FileSystem" -Persist

只要重启电脑,就会消失的驱动器持久化。改用 net use 命令可以解决这个问题:

net use x: \\some\path /persistent:Yes

所以这些命令看起来非常不等效。但是我在一些奇怪的企业 VPN 上,所以你可能会有不同的情况。


1
你对 -Persist 开关的理解是错误的。如 帮助页面 中所述,请确保指定了全局范围。
New-PSDrive -Name 'X' -PSProvider 'FileSystem' -Root '\\share\C$' -Scope 'Global' -Persist

-Persist [<SwitchParameter>]

Indicates that this cmdlet creates a Windows mapped network drive. Mapped network drives are saved in Windows on the local computer. They are persistent, not session-specific, and can be viewed and managed in File Explorer and other tools.

When you scope the command locally, that is, without dot-sourcing, the Persist parameter does not persist the creation of a PSDrive beyond the scope in which you run the command. If you run New-PSDrive inside a script, and you want the new drive to persist indefinitely, you must dot-source the script. For best results, to force a new drive to persist, specify Global as the value of the Scope parameterin addition to adding Persist to your command.

The name of the drive must be a letter, such as D or E. The value of Root parameter must be a UNC path of a different computer. The value of the PSProvider parameter must be FileSystem.

To disconnect a Windows mapped network drive, use the Remove-PSDrive cmdlet. When you disconnect a Windows mapped network drive, the mapping is permanently deleted from the computer, not just deleted from the current session.

Mapped network drives are specific to a user account. Mapped network drives that you create in sessions that are started by using the Run as administrator option or by using the credential of another user are not visible in a session that was started without explicit credentials, or by using the credentials of the current user.


我相信我没有准确地表达我的问题。我会立即修改它,使其更具体。 - Petr Synek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接