从 VBScript 中访问网络共享,例如 FileSystemObject [英] Access network share from within VBScript eg FileSystemObject

查看:31
本文介绍了从 VBScript 中访问网络共享,例如 FileSystemObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种使用替代凭据(不是运行 VBS 脚本的凭据)从 VBS 脚本中访问网络共享的好方法?

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)?

目的是执行两个任务:

  1. 以编程方式导航远程共享文件结构,以确认存在几个远程文件,并将一个文件复制到另一个文件(均为远程文件)
  2. 将文件从本地驱动器(使用本地用户名/权限访问)复制到远程驱动器(使用备用凭据访问)

据我所知 FSO (Scripting.FileSystemObject) 不在图片中,因为它总是使用使用它的应用程序的凭据运行 - 这将是本地计算机用户.(?)

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.(?)

我在使用net use"提供远程共享凭据的批处理文件(或对cmd.exe"的扩展调用)时发现的唯一可行的方法,然后使用 robocopy 复制文件或类似的,来自同一个命令外壳会话".这对于将文件从本地驱动器复制/部署到远程共享是可行的,但是以这种方式进行任何类型的文件系统浏览(就像您使用 FSO 所做的那样)会非常复杂和脆弱.

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.

我考虑过的另一种可能性涉及有两个脚本会话 - 您调用脚本(在命令行中提供备用凭据)并运行 cmd.exe 会话,该会话首先执行网络使用"以映射远程共享到临时本地驱动器,然后以实际操作"模式自行运行并使用 FSO,然后在完成后(返回 cmd.exe 外壳)再次将临时驱动器与net use"断开连接.这很笨拙(多个窗口,临时驱动器......),我什至不确定它是否有效.

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.

有人知道任何一种方式,或者知道可行的替代方案吗?(在 Windows 2000 机器上坚持使用 VBScript/WScript - 没有 PowerShell!)

Does anybody know either way, or know of a viable alternative? (sticking to VBScript / WScript on a windows 2000 machine - no PowerShell!)

推荐答案

好吧,我当时有一个误解 - FSO 不会获取"使用NET USE"(或 Wscript.NetworkMapNetworkDrive"建立的网络凭据)").

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):

ServerShare = "\192.168.3.56d$"
UserName = "domainusername"
Password = "password"

Set NetworkObject = CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")

NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password

Set Directory = FSO.GetFolder(ServerShare)
For Each FileName In Directory.Files
    WScript.Echo FileName.Name
Next

Set FileName = Nothing
Set Directory = Nothing
Set FSO = Nothing

NetworkObject.RemoveNetworkDrive ServerShare, True, False

Set ShellObject = Nothing
Set NetworkObject = Nothing

这篇关于从 VBScript 中访问网络共享,例如 FileSystemObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆