Access network share from within VBScript eg FileSystemObject

前端 未结 1 1209
刺人心
刺人心 2020-11-30 03:16

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 i

1条回答
  •  有刺的猬
    2020-11-30 03:56

    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
    

    0 讨论(0)
提交回复
热议问题