Announcement

Collapse
No announcement yet.

Change UNC Path to an unknown network drive letter

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Change UNC Path to an unknown network drive letter

    Hello everyone. I have been using Petri for a long time but this is my first post ever. This is a great site.
    I have always done batch files for mapping drives, etc. But my new job likes vb scripts.

    We have a network share \\fileserver1\share that users have mapped individually/manually to any drive letter of there liking , not thru a login script.

    I have created a network share \\fileserver2\share that I want these users to start using. It is the same data as above just on a different, newer server.

    I need a script that will look for a mapped network drive ( again unknown letter ) that has a UNC path of \\fileserver1\share and will then remap it to \\fileserver2\share will no user interaction and it will hold permanently.

    Any help is appreciated.


    Thank you,
    Jarmar

  • #2
    Re: Change UNC Path to an unknown network drive letter

    jarmar,

    paste this in a new text file, and rename the extension to vbs.
    (don't forget to change the variables to match your needs)

    Code:
    On Error Resume Next
    Dim oNetwork, oDrive, iDrive, bKeepInProfile
    Dim sDriveLetter, sOldPath, sNewPath
    
    'Variables:
    sDriveLetter = "X:"
    sOldPath = "\\OLDSERVER\OLDSHARE"
    sNewPath = "\\NEWSERVER\NEWSHARE"
    bKeepInProfile = True 'False = Mapping will not be stored in the user Profile
    
    'Create objects:
    Set oNetwork = CreateObject("WScript.Network") 
    Set oDrive = oNetwork.EnumNetworkDrives
    
    'Disconnect the mapped drive first:
    If oDrive.Count > 0 Then
    	For iDrive = 0 to oDrive.Count - 1
    		If UCase(oDrive.Item(iDrive)) = UCase(sOldPath) Then 
    			oNetwork.RemoveNetworkDrive (oDrive.Item(iDrive-1))
    		End If
    	Next
    End If
    
    'Map the network path we need:
    oNetwork.MapNetworkDrive sDriveLetter, sNewPath, bKeepInProfile
    
    'Clean up...
    Set oDrive = Nothing
    Set oNetwork = Nothing
    hope it helps...
    martin77.
    Last edited by martin77; 11th September 2006, 17:45.

    Comment


    • #3
      Re: Change UNC Path to an unknown network drive letter

      Thank you. It did map the drive but it did not remove the old drive. For instance.

      I mapped

      M: to \\fileserver1\share (other users may have mapped x: or z: , etc)

      When i run your script it gave me an X: drive. It did not remap the M: to \\fileserver2\share

      The one variable is that everyone has various drive letters that they have mapped to \\fileserver1\share locally on their desktop.

      Thanks for your assistance.

      Comment


      • #4
        Re: Change UNC Path to an unknown network drive letter

        OK, my mistake...

        just add this line:
        Code:
        sDriveLetter = oDrive.Item(iDrive-1)
        between this (line 19):
        Code:
        		If UCase(oDrive.Item(iDrive)) = UCase(sOldPath) Then 
        			--- ADD NEW LINE HERE ---
        			oNetwork.RemoveNetworkDrive (oDrive.Item(iDrive-1))
        		End If
        It will save the old drive letter and map the new drive to it.

        good luck,
        martin77.

        Comment


        • #5
          Re: Change UNC Path to an unknown network drive letter

          Thank you again. I am an idiot when it comes to scripts ( I am sure you can tell )

          Do I indent it, or anything? Also what about the X: mapping at the top.

          If I run it by pasting it in right under the line 19 it still maps the X: and doesn't change the other drive or map it to the new location. I really appreciate your help.

          Comment


          • #6
            Re: Change UNC Path to an unknown network drive letter

            How would you do this with multiple users that you want to change...like hundreds. Read from a text file? How would the format go with that?

            Comment


            • #7
              Re: Change UNC Path to an unknown network drive letter

              It is going to go into a domain wide login script. I was hoping it would look for the unc path in one of mapped network drives and change the UNC path. this way it would only effect the users that had a mapping of that unc path.

              Comment


              • #8
                Re: Change UNC Path to an unknown network drive letter

                OK... try this:
                (Note that the 1st line ("On Error Resume Next") was remarked, this way you'll know if the script encountered any errors...)

                Code:
                'On Error Resume Next
                Dim oNetwork, oDrive, iDrive, bKeepInProfile
                Dim sDriveLetter, sOldPath, sNewPath
                
                'Variables:
                sDriveLetter = ""
                sOldPath = "\\OLDSERVER\OLDSHARE"
                sNewPath = "\\NEWSERVER\NEWSHARE"
                bKeepInProfile = True 'False = Mapping will not be stored in the user Profile
                
                'Create objects:
                Set oNetwork = CreateObject("WScript.Network") 
                Set oDrive = oNetwork.EnumNetworkDrives
                
                'Disconnect the mapped drive first:
                If oDrive.Count > 0 Then
                	For iDrive = 1 to oDrive.Count - 1
                		WScript.Echo oDrive.Item(iDrive-1) & " = " & oDrive.Item(iDrive)
                		If UCase(oDrive.Item(iDrive)) = UCase(sOldPath) Then
                			sDriveLetter = oDrive.Item(iDrive-1)
                			oNetwork.RemoveNetworkDrive (oDrive.Item(iDrive-1))
                		End If
                	Next
                End If
                
                'Map the network path we need, (only if it was mapped before):
                If sDriveLetter <> "" Then oNetwork.MapNetworkDrive sDriveLetter, sNewPath, bKeepInProfile
                
                'Clean up...
                Set oDrive = Nothing
                Set oNetwork = Nothing
                it will show you all the mapped drives, and map the one you need ONLY if it was mapped before.

                just make sure you run the script in CSCRIPT and not in WSCRIPT
                (otherwize, you'll get a MessageBox for each mapped drive you have)
                and again, don't forget to change the "sOldPath " and "sNewPath" variables to match your needs.

                Comment


                • #9
                  Re: Change UNC Path to an unknown network drive letter

                  Martin77. I appreciate your help... again
                  I ran it in wscript cause I don't have that many drives. When I login the M: drive where this manually done share shows disconnected network drive. When I run the script you created like that it doesn't show in the list during the popups. If I click on the current M: drive then run the script again it shows the mapping for M: then says
                  Line: 27
                  Char: 28
                  Error: The local device name has a remembered connection to another network resource.

                  Code: 800704B2
                  Source: WSHNetwork.MapNetworkDrive

                  As always thanks

                  Comment


                  • #10
                    Re: Change UNC Path to an unknown network drive letter

                    Martin77, are you out there? I really need your help on this.

                    Comment


                    • #11
                      Re: Change UNC Path to an unknown network drive letter

                      Martin77, way to leave a guy hangin man.

                      Comment


                      • #12
                        Re: Change UNC Path to an unknown network drive letter

                        Maybe he doesn't have time to answer.
                        But why not keep it simple and create a batchfile?

                        Net use * /delete
                        Net use M: \\server\share1
                        Net use N: \\server\share2
                        etcetc
                        Marcel
                        Technical Consultant
                        Netherlands
                        http://www.phetios.com
                        http://blog.nessus.nl

                        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                        "No matter how secure, there is always the human factor."

                        "Enjoy life today, tomorrow may never come."
                        "If you're going through hell, keep going. ~Winston Churchill"

                        Comment


                        • #13
                          Re: Change UNC Path to an unknown network drive letter

                          Sorry for the delay... I've been kinda busy these days... (I got married!!! )
                          here is the fixed code you need:

                          Code:
                          'On Error Resume Next
                          Dim oNetwork, oDrive, iDrive, bKeepInProfile
                          Dim sDriveLetter, sOldPath, sNewPath
                          
                          'Variables:
                          sDriveLetter = ""
                          sOldPath = "\\OLDSERVER\OLDSHARE"
                          sNewPath = "\\NEWSERVER\NEWSHARE"
                          bKeepInProfile = True 'False = Mapping will not be stored in the user Profile
                          
                          'Create objects:
                          Set oNetwork = CreateObject("WScript.Network") 
                          Set oDrive = oNetwork.EnumNetworkDrives
                          
                          'Disconnect the mapped drive first:
                          If oDrive.Count > 0 Then
                          	WScript.Echo "Looking For " & UCase(sOldPath) & " to remove..."
                          	For iDrive = 1 to oDrive.Count - 1
                          		WScript.Echo oDrive.Item(iDrive-1) & " = " & UCase(oDrive.Item(iDrive))
                          		If UCase(oDrive.Item(iDrive)) = UCase(sOldPath) Then
                          			sDriveLetter = oDrive.Item(iDrive-1)
                          			WScript.Echo "Disconnecting Mapped Drive "  & sDriveLetter & " ..."
                          			oNetwork.RemoveNetworkDrive oDrive.Item(iDrive-1), True, True
                          		End If
                          	Next
                          Else
                          	WScript.Echo "No network drives mapped were found!"
                          End If
                          
                          'Map the network path we need, (only if it was mapped before):
                          If sDriveLetter <> "" Then
                          	WScript.Echo "Connecting " & sDriveLetter & " to " & sNewPath
                          	oNetwork.MapNetworkDrive sDriveLetter, sNewPath, bKeepInProfile
                          Else
                          	WScript.Echo "Old path not found, will not map new drive"
                          End If
                          
                          'Clean up...
                          Set oDrive = Nothing
                          Set oNetwork = Nothing
                          I've tested it on WinXP and Win2K.
                          good luck,
                          Martin77

                          Comment


                          • #14
                            Re: Change UNC Path to an unknown network drive letter

                            Originally posted by martin77
                            Sorry for the delay... I've been kinda busy these days... (I got married!!! )
                            Martin77
                            Congratulations Martin.
                            1 1 was a racehorse.
                            2 2 was 1 2.
                            1 1 1 1 race 1 day,
                            2 2 1 1 2

                            Comment


                            • #15
                              Re: Change UNC Path to an unknown network drive letter

                              Congratulations Martin.. Where's the beer???
                              Marcel
                              Technical Consultant
                              Netherlands
                              http://www.phetios.com
                              http://blog.nessus.nl

                              MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                              "No matter how secure, there is always the human factor."

                              "Enjoy life today, tomorrow may never come."
                              "If you're going through hell, keep going. ~Winston Churchill"

                              Comment

                              Working...
                              X