无法在以管理员身份运行的 PowerShell 中访问网络驱动器
我在 Windows 7 x64 虚拟机中运行 PowerShell。我在主机上有一个映射为网络驱动器 (Z:) 的共享文件夹。当我正常运行 PS 时,我可以很好地访问该驱动器,但如果我“以管理员身份”运行它,它会告诉我:
Set-Location : Cannot find drive. A drive with the name 'Z' does not exist.
At line:1 char:13
+ Set-Location <<<< Z:
+ CategoryInfo : ObjectNotFound: (Z:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
如何以管理员身份访问网络驱动器?
I'm running PowerShell in a Windows 7 x64 virtual machine. I have a shared folder on the host mapped as a network drive (Z:). When I run PS normally I can access that drive just fine, but if I run it "as administrator" it tells me:
Set-Location : Cannot find drive. A drive with the name 'Z' does not exist.
At line:1 char:13
+ Set-Location <<<< Z:
+ CategoryInfo : ObjectNotFound: (Z:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
How do I access network drives as administrator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最后,修复方法只是在以管理员身份运行时重新映射驱动器盘符:
它不必从同一个 PowerShell 实例(或根本从 PowerShell)完成 - 这只是一些事情这需要在整个登录会话中完成一次。
In the end the fix was simply to re-map the drive letter while running as Administrator:
It doesn't have to be done from the same PowerShell instance (or from PowerShell at all) - it's just something that needs to be done once for the entire logon session.
就我而言,我可以简单地使用 UNC 路径而不是驱动器映射,并且效果很好。
因此,根据您的示例,我没有使用映射驱动器 Z:\,而是使用“\\vmware-host\Shared Folder”作为路径。
In my case, I was able to simply use the UNC path instead of the drive mapping and it worked fine.
So, per your example, instead of using the mapped drive Z:\, I just used "\\vmware-host\Shared Folder" as the path.
我花了很长时间才找到的另一种解决方法是从计划任务作为 NT AUTHORITY\SYSTEM 帐户运行
net use
。显然 此帐户下映射的驱动器向所有用户和所有海拔级别显示。我已经对此进行了测试,它甚至可以在 NFS 共享上运行(这可能有点挑剔)。只需创建一个在系统启动时运行的计划任务,并指定常用命令:
使用
/persistent:yes
运行一次可能会起作用,但我还没有尝试过。当然,“只需再次映射它”也可以,但该驱动器对于在不同上下文中运行的计划任务仍然不可见。缺点是所有真实用户也会看到它,因此不适合多用户设置。One other work-around that took me ages to find is to run
net use
from a scheduled task as the NT AUTHORITY\SYSTEM account. Apparently drives mapped under this account show up for all users and all elevation levels.I've tested this and it works even on NFS shares (which can be a bit finicky). Just create a scheduled task set to run at system startup, and specify the usual command:
It might possibly work to run it just once with
/persistent:yes
, but I haven't tried that. Granted, "just map it again" works too, but that drive still won't be visible to scheduled tasks running in different contexts. The downside is that all real users see it too, so not so good for multiuser setups.我正在使用以下 hacky 解决方案,当 Powershell 在提升模式下运行时,我在 profile.ps1 中重新创建“丢失”的 PSDrive。
要点
I'm using the following hacky solution where I recreate "missing" PSDrives in profile.ps1 when Powershell is running in elevated mode.
Gist
如果 Powersheel 以管理员身份运行,我们可以从注册表中获取有关现有映射驱动器的信息,并通过在 $PROFILE 中包含以下内容在管理会话中重新创建它们。
If Powersheel runs as Admin we may get the info about the existing mapped drives from the registry and recreate them in the admin session by including the following in the $PROFILE.
映射一个新的 psdrive 来访问该数据怎么样?当您在 powershell 中编写脚本或访问网络数据存储时,PSDrive 的工作效果与系统映射驱动器一样好,甚至更好。
使用 New-PSDrive cmdlet 的说明如下:Technet :New-PSDrive
如果您不想每次都创建一个新的 psdrive,您可以将其添加到管理员和您的用户帐户的配置文件中,并且每次打开 powershell 时它都会自动可用。
~丹
How about mapping a new psdrive to acess that data? PSDrives work just as well if not better than system mapped drives when you are writing scripts or accessing network data stores in powershell.
Instructions for using the New-PSDrive cmdlet are here: Technet:New-PSDrive
If you don't want to have to make a new psdrive every time you could add it to the profiles for both the Administrator and your user account and it will automatically be available every time you open powershell.
~Dan
自 Vista 以来,这似乎是 Microsoft 的一个已知问题。
Microsoft 知识库文章,其中包含不安全注册表修复。
我们目前正在评估这种方法,因为我们的一些人感觉机器在此之后可能无法启动;-)
Seems like a known problem to Microsoft since Vista.
Microsoft Knowled base article with unsafe registry fix.
We are currently evaluating this approach as some of our guys have feelings that machine may not start after this ;-)
其他答案都不适合我;但@TimothyLeeRussell 的答案为我指明了正确的方向。
就我而言,我的网络驱动器上有一个
.bat
文件。当我以管理员身份运行它时,它只会闪烁一个命令提示符窗口,然后立即消失;当我从提升的命令提示符运行文件内容时,它工作得很好。最后我意识到我尝试从映射的网络驱动器运行
.bat
文件。我将文件的执行更改为使用 UNC 路径,并且它有效。None of the other answers worked for me; but @TimothyLeeRussell's answer pointed me in the right direction.
In my case, I had a
.bat
file located on a network drive. When I ran it as administrator it would just flash a command prompt window and instantly disappear; it worked fine when I ran the file's contents from an elevated command prompt.Finally I realized that I tried running the
.bat
file from the mapped network drive. I changed the execution of the file to use the UNC path and it worked.