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

查看:111
本文介绍了从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.(?)

在谷歌搜索准备一个批处理文件(或对"cmd.exe"的扩展调用)时发现的唯一可行的方法,该批处理文件使用"net use"提供远程共享凭据,然后使用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会话,该会话首先执行"net use"来映射远程共享到临时本地驱动器,然后以实际完成任务"模式运行自己并使用FSO,然后在完成操作后(返回cmd.exe shell)再次通过"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.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):

ServerShare = "\\192.168.3.56\d$"
UserName = "domain\username"
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天全站免登陆