Access network share from within VBScript eg FileSystemObject

为君一笑 提交于 2019-11-26 10:55:51

问题


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:

  1. 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)
  2. 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!)


回答1:


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


来源:https://stackoverflow.com/questions/631401/access-network-share-from-within-vbscript-eg-filesystemobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!