哪个是“更好”?在Powershell中映射驱动器的方法?
我正在尝试优化登录脚本,使其尽可能健壮且快速执行。
该脚本记录了许多驱动器,我可以看到我可以使用:
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用“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