Jump to content
Sign in to follow this  
antmar904

Re-mapping a network drive to a diffrent location

Recommended Posts

antmar904
hi all,
 
i have a network share that will be changing to a different share path, i wanted to have a script that will:
 
1 find the drive letter that is mapping servershareold
 
2 disconnect servershareold
 
3 use that same drive letter and map serversharenew
 
any help is greatly appreciated

Share this post


Link to post
Share on other sites
ZacUSNYR

What are you having trouble with?  Many built in functions can handle this for you.  DriveGetDrive, DriveMapGet, DriveMapDel, and DriveMapAdd

Share this post


Link to post
Share on other sites
antmar904

i am having trouble trying to figure out how to get the current driver letter that is being mapped to servershareold and put that into a variable so can then use that driver letter and remap to the new share path

Edited by antmar904

Share this post


Link to post
Share on other sites
ZacUSNYR

One way to do it is to create a secondary 2d array, store the network drive letters in one index and use DriveMapGet and store the path.

; Edit these guys to change the mappings
Global $sOrigShare = "\\oldserver\oldshare"
Global $sNewShare = "\\newserver\newshare"
 
 
Global $asCurrentSMB[1][2] ; Internal 2d array to keep track of drive letters
$asGetDrives = DriveGetDrive("NETWORK") ; Get Network drives only
If Not @error Then ; If DriveGetDrive finds no network drives then display a MsgBox and exit
ReDim $asCurrentSMB[$asGetDrives[0] + 1][2] ; Resize array based on amount of drives found
$asCurrentSMB[0][0] = $asGetDrives[0] ; Keep index from DriveGetDrive
For $i = 1 To $asGetDrives[0] ; Populate 2d Array 
$asCurrentSMB[$i][0] = $asGetDrives[$i] ;Drive Letter
$asCurrentSMB[$i][1] = DriveMapGet($asGetDrives[$i]) ; Mapping
Next
Else
Exit MsgBox(0, "NOT FOUND", "Unable to locate any network shares.") ; Msgbox and Exiting
EndIf
 
For $i = 1 To $asCurrentSMB[0][0] ; Loop through the 2d array
If StringInStr($asCurrentSMB[$i][1], $sOrigShare) Then ; Find if server/shares match
ConsoleWrite("Found " & $asCurrentSMB[$i][0] & " mapped to " & $asCurrentSMB[$i][1]) ; Console information
If DriveMapDel($asCurrentSMB[$i][0]) Then ; Delete drive mapping
ConsoleWrite(".. Removed." & @CRLF)
; Readd and inform console
If DriveMapAdd($asCurrentSMB[$i][0], $sNewShare, 1) Then ConsoleWrite(" Share Created ... " & $asCurrentSMB[$i][1] & " mapped to " & $asCurrentSMB[$i][0] & @CRLF)
EndIf
EndIf
Next

Share this post


Link to post
Share on other sites
antmar904

thank you,

i changed the shares but the drive mapping did not change to the new share location.

no errors either.

Share this post


Link to post
Share on other sites
ZacUSNYR

If you open up command prompt and type 'net use' - is it changed?  On my Windows 7 machine it didn't change the display in explorer (assume it would after a restart but I use domain policy for network drives).

Edited by ZacUSNYR

Share this post


Link to post
Share on other sites
antmar904

oh yes it is but its not changed in windows explore

Share this post


Link to post
Share on other sites
jdelaney

Windows explorer is an odd one.  The actual new drive is used, but the display on the drive definition will be for the old map.

Not sure how to update it, and I never use explorer to check drives...always 'net use'

http://stackoverflow.com/questions/4940603/how-to-refresh-windows-explorer

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
ZacUSNYR

http://support.microsoft.com/kb/306117

I got the same behavior, and that registry value doesn't exist.  Restarting explorer fixes it.  

In 7 you can just kill explorer and it'll restart.

If ProcessExists("explorer.exe") Then ProcessClose("explorer.exe")

 

Could add a flag in the routine to only do that if something changed.

 
Global $sOrigShare = "\\bashful\general"
Global $sNewShare = "\\scooby\accshare"
 
Global $bUpdated = False
Global $asCurrentSMB[1][2] ; Internal 2d array to keep track of drive letters
$asGetDrives = DriveGetDrive("NETWORK") ; Get Network drives only
If Not @error Then ; If DriveGetDrive finds no network drives then display a MsgBox and exit
ReDim $asCurrentSMB[$asGetDrives[0] + 1][2] ; Resize array based on amount of drives found
$asCurrentSMB[0][0] = $asGetDrives[0] ; Keep index from DriveGetDrive
For $i = 1 To $asGetDrives[0] ; Populate 2d Array
$asCurrentSMB[$i][0] = $asGetDrives[$i] ;Drive Letter
$asCurrentSMB[$i][1] = DriveMapGet($asGetDrives[$i]) ; Mapping
Next
Else
Exit MsgBox(0, "NOT FOUND", "Unable to locate any network shares.") ; Msgbox and Exiting
EndIf
 
For $i = 1 To $asCurrentSMB[0][0] ; Loop through the 2d array
If StringInStr($asCurrentSMB[$i][1], $sOrigShare) Then ; Find if server/shares match
ConsoleWrite("Found " & $asCurrentSMB[$i][0] & " mapped to " & $asCurrentSMB[$i][1]) ; Console information
If DriveMapDel($asCurrentSMB[$i][0]) Then ; Delete drive mapping
ConsoleWrite(".. Removed." & @CRLF)
; Readd and inform console
If DriveMapAdd($asCurrentSMB[$i][0], $sNewShare, 1) Then
ConsoleWrite(" Share Created ... " & $sNewShare & " mapped to " & $asCurrentSMB[$i][0] & @CRLF)
$bUpdated = True
EndIf
EndIf
EndIf
Next
 
If $bUpdated Then
$sRegKey = RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer", "DesktopProcess")
If Not @error Then
ConsoleWrite("DesktopProcess registry key exists - Value : " & $sRegKey & @CRLF)
If RegDelete("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer", "DesktopProcess") Then ConsoleWrite(" Deleted." & @CRLF)
Else
ConsoleWrite("DesktopProcess registry key does not exist." & @CRLF)
EndIf
 
If ProcessExists("explorer.exe") Then ProcessClose("explorer.exe")
EndIf

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×