从 VBScript 中访问网络共享,例如 FileSystemObject
有没有一种好方法可以使用替代凭据(而不是运行 VBS 脚本所使用的凭据)从 VBS 脚本内访问网络共享?
目的是执行两项任务:
- 以编程方式导航远程共享文件结构,以确认存在几个远程文件,并
- 从本地驱动器(使用本地用户名访问)将一个文件 复制到另一个(都是远程)文件上/ 权限)到远程驱动器(使用备用凭据访问)
据我所知,FSO(Scripting.FileSystemObject)不适用,因为它始终使用使用它的应用程序的凭据运行 - 这将是本地机器用户。(?)
我在谷歌搜索准备批处理文件(或对“cmd.exe”的扩展调用)时发现的唯一看似可行的方法,该文件使用“net use”提供远程共享凭据,然后复制来自同一命令外壳“会话”内的带有 robocopy 等的文件。 这对于将文件从本地驱动器复制/部署到远程共享来说是可行的,但以这种方式进行任何类型的文件系统浏览(就像使用 FSO 一样)会非常复杂且脆弱。
我考虑过的另一种可能性涉及有两个脚本会话 - 您调用脚本(在命令行中提供备用凭据),它运行一个 cmd.exe 会话,该会话首先执行“net use”将远程共享映射到临时共享本地驱动器,然后以“实际执行操作”模式自行运行并使用 FSO,完成后(返回 cmd.exe shell)再次使用“net use”断开临时驱动器的连接。 这很笨重(多个窗口,临时驱动器......),我什至不确定它是否有效。
有人知道这两种方式,或者知道可行的替代方案吗? (在 Windows 2000 机器上坚持使用 VBScript / WScript - 没有 PowerShell!)
Is there a good way to access network shares from within a VBS script, with alternative credentials (not the credentials with which the VBS script is running)?
The intention is to perform two tasks:
- programmatically navigate a remote share file structure, in order to confirm that a couple of remote files exist, and copy one file over the other (both remote)
- copy files from a local drive (accessed with local username / permissions) to a remote drive (accessed with the alternate credentials)
As far as I can tell FSO (Scripting.FileSystemObject) is out of the picture, because it always runs with the credentials of the application using it - which would be the local machine user.(?)
The only viable-seeming approach I have found while googling to prepare a Batch file (or extended call to "cmd.exe") that uses "net use" to provide the remote share credentials, and then copies the files with robocopy or the like, from within the same command-shell "session". This would work OK for copying/deploying files from the local drive to the remote share, but it would ve very complicated and brittle to do any sort of file system browsing (like you would do with FSO) in this way.
Another possibility I have considered involves having two scripting sessions - you call the script (providing the alternate credentials in the command line) and it runs a cmd.exe session, which first does a "net use" to map the remote share to a temporary local drive, then runs itself in an "actually do stuff" mode and uses FSO, then when it's done (back in the cmd.exe shell) disconnects the temporary drive with "net use" again. This is clunky (multiple windows, temporary drive...) and I'm not even sure it would work.
Does anybody know either way, or know of a viable alternative? (sticking to VBScript / WScript on a windows 2000 machine - no PowerShell!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我一直在误解 - FSO 不会“获取”使用“NET USE”(或 Wscript.Network“MapNetworkDrive”)建立的网络凭据。
事实证明确实如此,并且以下示例代码运行得非常好(无需设置临时网络驱动器):
OK, I was laboring under a misconception - that FSO would not "pick up" the network credentials established with "NET USE" (or Wscript.Network "MapNetworkDrive").
It turns out that it does, and the following sample code works very nicely (without needing to set up temporary network drives):