DFS Remap Script

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 16 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ruaand79
Posts: 12
Last visit: Thu Jan 24, 2008 1:10 am

DFS Remap Script

Post by ruaand79 »

Hi,

I found this script in scriptvault but am not sure how to use it. How do I get it to check if a V: drive is mapped and if it is mapped to remove it and then remap it to the DFS Share.

The write to text file option is not needed.

Please help

'migrate.vbs'Created by Michael Brierley 'Version 0.7c First BetaDim objWshNetwork, nFDim sShare, sDrive, objEnumNetworkDim objFSO, objTextFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("C:dfs.txt", true)
Set objWshNetwork = CreateObject("WScript.Network")
Set objEnumNetwork = objWshNetwork.EnumNetworkDrives
For nF = 0 To objEnumNetwork.Count - 1 Step 2 'get the drive letter and share name for the current share sShare = objEnumNetwork(nF) sDrive = objEnumNetwork(nF + 1) 'check if the current share is connected to fileserv1 'wscript.echo StrComp(Left(sShare, 14), "fileserv1", vbTextCompare) 'WScript.Echo Left(sDrive,14) & " sDrive" 'WScript.Echo Left(sShare,14) & " sShare" If StrComp(Left(sDrive, 14), "fileserv1", vbTextCompare) = 0 Then 'remove the existing share objWshNetwork.RemoveNetworkDrive sShare, True, True 'create text file for logging and enter the data objTextFile.WriteLine "Removed: " & sShare & " " & sDrive 'remap the drive to the new server objWshNetwork.MapNetworkDrive sShare, "DOMAINdfsroot" & Mid(sDrive,15), True objTextFile.WriteLine "Mapped: " & sShare & " DOMAINdfsroot" & Mid(sDrive,15) End If
NextobjTextFile.Close
User avatar
ruaand79
Posts: 12
Last visit: Thu Jan 24, 2008 1:10 am

DFS Remap Script

Post by ruaand79 »

Hi,

I found this script in scriptvault but am not sure how to use it. How do I get it to check if a V: drive is mapped and if it is mapped to remove it and then remap it to the DFS Share.

The write to text file option is not needed.

Please help

'migrate.vbs'Created by Michael Brierley 'Version 0.7c First BetaDim objWshNetwork, nFDim sShare, sDrive, objEnumNetworkDim objFSO, objTextFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("C:dfs.txt", true)
Set objWshNetwork = CreateObject("WScript.Network")
Set objEnumNetwork = objWshNetwork.EnumNetworkDrives
For nF = 0 To objEnumNetwork.Count - 1 Step 2 'get the drive letter and share name for the current share sShare = objEnumNetwork(nF) sDrive = objEnumNetwork(nF + 1) 'check if the current share is connected to fileserv1 'wscript.echo StrComp(Left(sShare, 14), "fileserv1", vbTextCompare) 'WScript.Echo Left(sDrive,14) & " sDrive" 'WScript.Echo Left(sShare,14) & " sShare" If StrComp(Left(sDrive, 14), "fileserv1", vbTextCompare) = 0 Then 'remove the existing share objWshNetwork.RemoveNetworkDrive sShare, True, True 'create text file for logging and enter the data objTextFile.WriteLine "Removed: " & sShare & " " & sDrive 'remap the drive to the new server objWshNetwork.MapNetworkDrive sShare, "DOMAINdfsroot" & Mid(sDrive,15), True objTextFile.WriteLine "Mapped: " & sShare & " DOMAINdfsroot" & Mid(sDrive,15) End If
NextobjTextFile.Close
User avatar
ruaand79
Posts: 12
Last visit: Thu Jan 24, 2008 1:10 am

DFS Remap Script

Post by ruaand79 »

I have done the following,

Set objNetwork = CreateObject("WScript.Network")

If StrComp("V:", kingcobra) = 0 Then 'remove the existing share objNetwork.RemoveNetworkDrive "V:", True, True Else 'remap the drive to the new server objNetwork.MapNetworkDrive "V:", DOMAINdfsroot End If

It works 100% if there is no V: drive mapping but if I create a mapped drive to kingcobra and run the script it does not want to disconnect the current V: mapping and remap a new V: to DOMAINdfsroot.

Any ideas,

R

User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

DFS Remap Script

Post by jhicks »

I just realized you have a logic error. You only map the new drive if the old one exists. After you remove the old mapping there is no place where the new drive mapping is added.

Here's another approach plus the correct logic:

Code: Select all

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objNetwork=CreateObject("WScript.Network")

If objFSO.DriveExists("P:") Then
    objNetwork.RemoveNetworkDrive "P:",True,True
End If

objNetwork.MapNetworkDrive "p:","jdh-nvnaspublic"
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

DFS Remap Script

Post by jhicks »

That's to be expected. This code is logically incorrect. You only need to remove the drive if it already exists but you always want to map drive V:This is how your code should look:If StrComp("V:", kingcobra) = 0 Then 'remove the existing share objNetwork.RemoveNetworkDrive "V:", True, True 'give operation a moment to finish wscript.sleep 200end if 'remap the drive to the new server objNetwork.MapNetworkDrive "V:", DOMAINdfsroot
This topic is 16 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked