哪个是“更好”?在Powershell中映射驱动器的方法?

发布于 2024-09-09 02:40:35 字数 496 浏览 0 评论 0原文

我正在尝试优化登录脚本,使其尽可能健壮且快速执行。

该脚本记录了许多驱动器,我可以看到我可以使用:

(New-Object -ComObject WScript.Network).MapNetworkDrive("X:", \\myserver\myshare))

net use x: \\myserver\myshare

来映射驱动器。

哪个是“更好”的方法? “传统”方式似乎更稳健,但比新方法慢。

当我测试驱动器是否存在时,我也遇到了一些奇怪的情况。即使它们显示在资源管理器中,有时当我测试时:

if (test-path x:) { write "Drive mapped already" }

它不会输出任何结果。这似乎只在某些时候发生,但我还没有弄清楚其中的模式。

谢谢,

I am trying to optimise a login script to make it as robust and quick to execute as possible.

The script logs a number of drives, and I can see I can use either:

(New-Object -ComObject WScript.Network).MapNetworkDrive("X:", \\myserver\myshare))

or

net use x: \\myserver\myshare

To map the drives.

Which is the "better" way? The "traditional" way seems more robust, but is slower than the newer method.

I'm also getting some oddities when I test to see if the drives exist. Even though they show in Explorer, sometimes when I test with:

if (test-path x:) { write "Drive mapped already" }

it does not output any result. This seems to only happen some of the time, but I haven't worked out the pattern.

Thanks,

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2024-09-16 02:40:35

使用“net use”映射驱动器不会自动使其在 powershell 中显示为 PSDrive。不幸的是,它们不是同一件事。 Test-Path 仅检查 PSPath,它是 UNC 路径或指向 powershell 提供程序的 PSDrive(或提供程序限定的文件系统::c:)。

这是 powershell 不是登录脚本的最佳 shell 的众多原因之一。

-奥辛

Mapping a drive with "net use" does not automatically make it visible in powershell as a PSDrive. They are not the same thing unfortunately. Test-Path checks only a PSPath, which is either a UNC path or a PSDrive (or provider-qualified like filesystem::c:) pointing at a powershell provider.

This is one in a long line of reasons why powershell is not the best shell for login scripts.

-Oisin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文