Access Mapped Drive that uses a different user credentials

Is there anyway to connect to a mapped drive that is configured to use different user credentials?
In this case the user credentials as not domain credentials but local credentials for the remote server.

I think this page explains how to achieve what you’re looking for.

When I mapped the share with different credentials while logged in as the service account. I can access the documents via Windows File Explorer.

Within Workflow, the Folder Capture does not see the mapped drives. Not even after logging out and back in.

You’ll have to dynamically map the drive from Workflow’s Startup process, otherwise it won’t see what’s been mapped by the user that’s currently logged on.

Option Explicit
Dim WshNetwork, oDrives, i
Dim objFSO, objNetwork, ShellObject
Dim ServerShare, UserName, Password, MappedDrive

ServerShare = "\\Server\Share"
UserName = "Domain\User"
Password = "Password"
MappedDrive = "T:"

ConnectAsDrive()
ListFiles()
CleanHouse()

Sub ConnectAsDrive()
    Set objNetwork = CreateObject("WScript.Network")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set WshNetwork = CreateObject("WScript.Network")
    Set oDrives = WshNetwork.EnumNetworkDrives
    For i = 0 to oDrives.Count - 1 Step 2
        if oDrives.Item(i) = MappedDrive then
            'Remove mapped drive if it exists
            objNetwork.RemoveNetworkDrive MappedDrive, True, True
            Watch.Log "MapDrive "  & oDrives.Item(i) & " = " & oDrives.Item(i+1) & " removed", 2
        End if
    Next
    'Create mapped drive with alternative credentials
    objNetwork.MapNetworkDrive MappedDrive, ServerShare, False, UserName, Password
    Watch.Log "MapDrive " & MappedDrive & " = " & ServerShare & " added", 2
End Sub

Sub ListFiles()
    Dim Directory, FileName
    Set Directory = objFSO.GetFolder(ServerShare)
    For Each FileName In Directory.Files
        Watch.log "MapDrive Listing " & FileName.Name, 3
    Next
    Set FileName = Nothing
    Set Directory = Nothing
End Sub

Sub CleanHouse()
    Set objFSO = Nothing
    Set ShellObject = Nothing
    Set objNetwork = Nothing
End Sub

Something like this?

  • Remove the mapped drive if it exists.
  • Add the mapped drive
  • List out the files just to verify that it works
  • Access the drive using a variable
  • Pull the files locally
  • Process normally

Well all you really need is a script in your Startup Process with these 2 lines of JS code:

var net = new ActiveXObject("WScript.Network");
net.MapNetworkDrive("T:", "\\\\Server\\Share", true,USERNAME,PASSWORD);

The rest is just extra precaution that assumes you will be monitoring your console every time the service starts.

1 Like

Definitely shorter.
I added a piece to remove the mapping prior to mapping again when the startup occurs again.
I like to keep the logs free of errors if possible.

var net = new ActiveXObject("WScript.Network");
var usr = Watch.GetVariable("usr");
var pwd = Watch.GetVariable("pwd");
net.RemoveNetworkDrive("T:", true, true);
net.RemoveNetworkDrive("P:", true, true);
net.MapNetworkDrive("T:", "\\\\Server\\Share", true,usr,pwd);
net.MapNetworkDrive("P:", "\\\\Server\\Share", true,usr,pwd);

I suppose should really test to see if it exists prior to trying to map though.

var net = new ActiveXObject("WScript.Network");
var usr = Watch.GetVariable("usr");
var pwd = Watch.GetVariable("pwd");
var myObject = new ActiveXObject("Scripting.FileSystemObject");

if(myObject.DriveExists("T")){
    Watch.Log("T: Drive Exists", 2);
} else {
    net.MapNetworkDrive("T:", "\\\\Server\\Share", true,usr,pwd);
    Watch.Log("T: Drive Mapped", 2);
}

if(myObject.DriveExists("P")){
    Watch.Log("P: Drive Exists", 2);
} else {
    net.MapNetworkDrive("P:", "\\\\Server\\Share", true,usr,pwd);
    Watch.Log("P: Drive Mapped", 2);
}

Actually, the third parameter for RemoveNetworkDrive() should be False so that the mapping isn’t forcibly removed from the user profile. This will save microseconds (hey, they all count! :stuck_out_tongue: ).

You shouldn’t test if the drives have already been mapped because even if they have, you have no way of telling if they are mapped to the actual shared resource you want to use. So simply using RemoveNetworkDrive() and MapNetworkDrive() is sufficient.

Good point and milliseconds do matter

    var net = new ActiveXObject("WScript.Network");
    var usr = Watch.GetVariable("usr");
    var pwd = Watch.GetVariable("pwd");
    net.RemoveNetworkDrive("T:", true, false);
    net.RemoveNetworkDrive("P:", true, false);
    net.MapNetworkDrive("T:", "\\\\Server\\Share", true,usr,pwd);
    net.MapNetworkDrive("P:", "\\\\Server\\Share", true,usr,pwd);

Thanks

1 Like