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.
Locked
User avatar
ruaand79
Posts: 12
Joined: Thu May 03, 2007 11:28 pm

DFS Remap Script

Post by ruaand79 » Mon May 14, 2007 2:16 am

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
Joined: Thu May 03, 2007 11:28 pm

DFS Remap Script

Post by ruaand79 » Mon May 14, 2007 2:16 am

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
Joined: Thu May 03, 2007 11:28 pm

DFS Remap Script

Post by ruaand79 » Mon May 14, 2007 2:42 am

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
Joined: Sun Jan 21, 2007 11:31 pm

DFS Remap Script

Post by jhicks » Mon May 14, 2007 5:46 am

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
Joined: Sun Jan 21, 2007 11:31 pm

DFS Remap Script

Post by jhicks » Mon May 14, 2007 6:34 am

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

Locked