Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help 2

Status
Not open for further replies.

Jeepix

MIS
Aug 21, 2003
102
US
Hi, all. I'm new to vbscript and need some assistance, please. I found the script below and tweaked it a little to work in our environment. We're migrating file servers to a new server with a different name, so we have to change any static drive mappings from the old server name to the new.

Below works as it should, but I need it to ignore the H: drive. I still want to change any other drive letters, just don't want to touch the H: drive because it'll remove it, remap it to point to the new server name as a 'persistent' drive instead of non-persistent. I guess making sure the script maps the drive the same way (non-persistent or persistent) would work, too, but just ignoring H: might be easier, no?

Anyway, any help would be greatly appreciated!
Thanks in advance

strOldServer = "\\oldserver_name\"
strNewServer = "\\newserver_name\"
Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For i = 0 To objDrives.Count - 1 Step 2
strDrive = objDrives(i)
strPath = objDrives(i + 1)
If Left(LCase(strPath), Len(strOldServer)) = strOldServer Then
strNewPath = Replace(strPath, strOldServer, strNewServer, vbTextCompare)
'WScript.Echo "Disconnecting " & strDrive & " from " & strPath & " and mapping it to " & strNewPath
On Error Resume Next
objNetwork.RemoveNetworkDrive strDrive, True, True
WScript.Sleep 3000
Err.Clear
On Error GoTo 0
objNetwork.MapNetworkDrive strDrive, strNewPath, True
End If
Next
 
Not tested, but ...

Quick and dirty way:
If [highlight #FCE94F](strDrive <> "H:") And[/highlight] Left(LCase(strPath), Len(strOldServer)) = strOldServer Then

Better would be:
Code:
If Left(LCase(strPath), Len(strOldServer)) = strOldServer Then
[highlight #FCE94F]   If strDrive = "H:" Then
      bPersistent = False
   Else
      bPersistent = True
   End If[/highlight]

...
   objNetwork.MapNetworkDrive strDrive, strNewPath, [highlight #FCE94F]bPersistent[/highlight]
 
Upon a quick test this appears to work perfectly (your 'best' way to do it). Going to test it a bit more.

Thank you SO much! Owe you a beer! [thumbsup]
-j
 
Update: I added the 'ignore' part as well as the non/-persistent test to give both choices and both are working flawlessly.

Thanks again!
-j
 
New requirement and I'm totally lost on this one. Any way to pull whether the mapped connection was persistent or non-persistent and re-map it the same way instead of stipulating manually?

It is set above to specify, but I just found out that we have a much of mappings in GP that they didn't tell us about. :(

<banging head against wall>

Thanks VERY MUCH again in advance!
-j

Current script:
' Remapping cscript. Renames servers (oldserver name to newserver name) for drive mappings on the fly, will retain drive letter of
' mapping changed, can stipulate drive letter(s) to be remapped as non-persistent, can stipulate drive letter(s) to be ignored.
' -Will remap live, on the fly, no need to reboot after script is finished
' -Will remove any old static mappings that do not exist in the new location (because failure to map windows drops them)
' -Will , if specified, remap to non-persistent (default is persistent) no matter what the previous mapping connection


' CHANGE strOldServer to Old Server Name keeping "\\" and "\" in the front and back, respectively
strOldServer = "\\old_server_name\"
' CHANGE strNewServer to New Server Name keeping "\\" and "\" in the front and back, respectively
strNewServer = "\\new_server_name\"


Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For i = 0 To objDrives.Count - 1 Step 2
strDrive = objDrives(i)
strPath = objDrives(i + 1)

' OPTIONAL Use next "If Statement" if you wish to "ignore" a drive letter. Replace H: with appropriate drive letter. Remark out line after next if used.
' If only one drive is needed remove "And (strDrive <> "Y:")", if more are needed add And (strDrive <> "Y:") and replace Y: with drive letter

' If (strDrive <> "H:") And (strDrive <> "Y:") And Left(LCase(strPath), Len(strOldServer)) = strOldServer Then

' Testing mapping to see if old server name exists (strOldServer). If you need to ignore certain drive letters then use line above INSTEAD and
' comment out the line below.

If Left(LCase(strPath), Len(strOldServer)) = strOldServer Then

' CHANGE Define whether a drive letter needs to be mapped NON-PERSISTENT

If (strDrive = "H:") Then
bPersistent = False

' CHANGE Extra drives can be added using the format below (2 lines- remove the ' (comment) from beginning of line if used) if needed to remain non-persistent:
' (NOTE: If no extra drives need to be remapped as non-persistent, then you may skip this change)
' Elseif strDrive = "Y:" Then
' bPersistent = False

' Next 3 lines will make all other drive letters not stipulated above to be non-persistent to be remapped PERSISTENT:
Else
bPersistent = True
End If

' Here we will remove old mapping and remap using same drive letter and path replacing only the old server name (strOldServer) with new server name (strNewServer)
strNewPath = Replace(strPath, strOldServer, strNewServer, vbTextCompare)
'WScript.Echo "Disconnecting " & strDrive & " from " & strPath & " and mapping it to " & strNewPath
On Error Resume Next
objNetwork.RemoveNetworkDrive strDrive, True, True
WScript.Sleep 3000
Err.Clear
On Error GoTo 0
objNetwork.MapNetworkDrive strDrive, strNewPath, bPersistent
End If
Next
 
You can use WMI for that. This function isn't well tested but should work.

Code:
Function IsPersistent(strDrive)
   IsPersistent = False

   Dim objWMIService, colItems
   Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
   Set colItems = objWMIService.ExecQuery(_
      "Select * From Win32_NetworkConnection WHERE LocalName = '" & LCase(strDrive) & "' And Persistent = True")

   For Each objItem in colItems
      IsPersistent = True
   Next

End Function

For more about Win32_NetworkConnection, see here
 
Thanks for the quick reply, guitarzan! I found something similar after doing hours of searching, but couldn't get it or your suggestion to work, how do I fit it into my script so that 'bPersistent' at the very end is set to true or false? I'm afraid this part is waaay over my head! If you can help me again I'd gladly buy you a little something, say from your Amazon wish list! :)

Thank you!
-j
 
I was getting at something like this (again, not tested)

Code:
' Remapping cscript. Renames servers (oldserver name to newserver name) for drive mappings on the fly, will retain drive letter of
' mapping changed, can stipulate drive letter(s) to be remapped as non-persistent, can stipulate drive letter(s) to be ignored.
' -Will remap live, on the fly, no need to reboot after script is finished
' -Will remove any old static mappings that do not exist in the new location (because failure to map windows drops them)
' -Will , if specified, remap to non-persistent (default is persistent) no matter what the previous mapping connection


' CHANGE strOldServer to Old Server Name keeping "\\" and "\" in the front and back, respectively
strOldServer = "\\old_server_name\"
' CHANGE strNewServer to New Server Name keeping "\\" and "\" in the front and back, respectively
strNewServer = "\\new_server_name\"


Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For i = 0 To objDrives.Count - 1 Step 2
   strDrive = objDrives(i)
   strPath = objDrives(i + 1)

   ' OPTIONAL Use next "If Statement" if you wish to "ignore" a drive letter. Replace H: with appropriate drive letter. Remark out line after next if used.
   ' If only one drive is needed remove "And (strDrive <> "Y:")", if more are needed add And (strDrive <> "Y:") and replace Y: with drive letter

   ' If (strDrive <> "H:") And (strDrive <> "Y:") And Left(LCase(strPath), Len(strOldServer)) = strOldServer Then

   ' Testing mapping to see if old server name exists (strOldServer). If you need to ignore certain drive letters then use line above INSTEAD and
   ' comment out the line below.

   If Left(LCase(strPath), Len(strOldServer)) = strOldServer Then

      [highlight #FCE94F]bPersistent = IsPersistent(strDrive)[/highlight]

      ' Here we will remove old mapping and remap using same drive letter and path replacing only the old server name (strOldServer) with new server name (strNewServer)
      strNewPath = Replace(strPath, strOldServer, strNewServer, vbTextCompare)
      'WScript.Echo "Disconnecting " & strDrive & " from " & strPath & " and mapping it to " & strNewPath
      On Error Resume Next
      objNetwork.RemoveNetworkDrive strDrive, True, True
      WScript.Sleep 3000
      Err.Clear
      On Error GoTo 0
      objNetwork.MapNetworkDrive strDrive, strNewPath, bPersistent
   End If
Next 

Function IsPersistent(strDrive)
   IsPersistent = False

   Dim objWMIService, colItems
   Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
   Set colItems = objWMIService.ExecQuery(_
      "Select * From Win32_NetworkConnection WHERE LocalName = '" & LCase(strDrive) & "' And Persistent = True")

   For Each objItem in colItems
      IsPersistent = True
   Next

End Function

And, a virtual beer is fine [cheers]
 
[cheers]Guitarzan! I think you nailed it! Quick testing seems to be doing what I need.

Thank you VERY much- my offer still stands if you change your mind!

I think this script will help a lot of people in the same situation once finished, so I'll post it for all.

I'll update later...

Thanks again!
-j
 
Your welcome. Please do post your working code if you can.

The other way to show thanks is clicking the "Like this post? Star it" link... it alerts other members that the problem had helpful responses. And of course, if you have anything to add to this forum, or the many other forums here that you may have familiarity with, it is always be appreciated.
 
Here's the finished/tested script. I hope this helps someone else on their next file server migration project!

Special BIG Thank you to guitarzan for all his help!!!

' Remapping cscript. Renames servers (oldserver name to newserver name) for drive mappings on the fly, will retain drive letter of
' mapping changed, will retain connection type (non-/persistent), can stipulate drive letter(s) to be ignored.
' -Will remap live, on the fly, no need to reboot after script is finished
' -Will remove any old static mappings that do not exist in the new location (because failure to map windows will drop them)
' -Will maintain persistent or non-persistent based on registry setting for network drives being remapped
' -Tested Win7/WinXP
' -Look in script below for the word "CHANGE" to modify script for the environment


' CHANGE strOldServer to Old Server Name keeping "\\" and "\" in the front and back, respectively
strOldServer = "\\old_server_name\"
' CHANGE strNewServer to New Server Name keeping "\\" and "\" in the front and back, respectively
strNewServer = "\\new_server_name\"


Function IsPersistent(strDrive)
isPersistent = False

Dim objWMIService, colItems
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * From Win32_NetworkConnection WHERE LocalName = '" & LCase(strDrive) & "' And Persistent = True")

For Each objItem in colItems
isPersistent = True
Next

End Function

Set objNetwork = CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For i = 0 To objDrives.Count - 1 Step 2
strDrive = objDrives(i)
strPath = objDrives(i + 1)

' OPTIONAL Use next "If Statement" if you wish to "ignore" a drive letter. Replace H: with appropriate drive letter. Remark out line after next if used.
' If only one drive is needed remove "And (strDrive <> "Y:")", if more are needed add And (strDrive <> "X:") before 'And Left(...' and replace X: with drive letter

' If (strDrive <> "N:") And (strDrive <> "Y:") And Left(LCase(strPath), Len(strOldServer)) = strOldServer Then

' Testing mapping to see if old server name exists (strOldServer). If you need to ignore certain drive letters then use line above INSTEAD and
' comment out the line below.

If Left(LCase(strPath), Len(strOldServer)) = strOldServer Then

bPersistent = IsPersistent(strDrive)

' Here we will remove old mapping and remap using same drive letter, path and persistence replacing only the old server name (strOldServer) with new server name (strNewServer)
strNewPath = Replace(strPath, strOldServer, strNewServer, vbTextCompare)
'WScript.Echo "Disconnecting " & strDrive & " from " & strPath & " and mapping it to " & strNewPath
On Error Resume Next
objNetwork.RemoveNetworkDrive strDrive, True, True
WScript.Sleep 3000
Err.Clear
On Error GoTo 0
objNetwork.MapNetworkDrive strDrive, strNewPath, bPersistent
End If
Next

' Test for Persistent drive mapping. This will modify bPersistent on line above (4 lines above) to persistent or non-persistent depending on setting in registry.

Function IsPersistent(strDrive)
IsPersistent = False

Dim objWMIService, colItems
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * From Win32_NetworkConnection WHERE LocalName = '" & LCase(strDrive) & "' And Persistent = True")

For Each objItem in colItems
IsPersistent = True
Next

End Function
 
Doing some testing on another script for renaming .lnk's related to the above script and is in another thread on this forum, I discovered a case-sensitivity issue that appears to affect this script as well.

If I have any mappings that are done in either partial or all caps (not all lower-case) the script will 'see' the mapping, remove it as if it's going to remap the server, but then it comes back with the same server name instead of replacing it with the new server name.

I.E.

\\SERVER01\share is not changed nor
\\Server01\share is changed, only
\\server01\share mapped drive is changed. Because it's all lower case.

Any way to have the script ignore case sensitivity? I'm sure the script will run across this issue as there are no standards for how to map the drives. :/

Help please!
Thanks in advance!

-Jeepix
 
Replace this:
strNewPath = Replace(strPath, strOldServer, strNewServer, vbTextCompare)
with this:
strNewPath = Replace(strPath, strOldServer, strNewServer, 1, -1, 1)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
One thing to be aware of when changing servers is that many of your Word documents may have template links pointing to locations on the old server. When templates other than Word’s Normal template are used to create a document, the template’s path & name are stored with the document. If that path is a network path, a change to the server name will break the link. The result can be significant delays in opening the documents on the new server. See: The same effect occurs when the file is opened on a computer attached to a different network. To fix that, you need to edit the template references in the documents concerned. For code to update the template references, see:
Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top