shusta Posted April 12, 2024 Posted April 12, 2024 (edited) Hello, I need a script that a user can run from the desktop that will remove or disconnect them from a network drive and then use the same letter and map to a different/new location. From searching the Internet I was able to create these two commands: Set objNetwork = CreateObject("WScript.Network")objNetwork.RemoveNetworkDrive "O:" , True Set objNetwork = CreateObject("WScript.Network")objNetwork.MapNetworkDrive "O:" , "\\servername\foldername" Each command works on it's own. My problem is not know what syntax needs to be added to make this one fluid script. Thanks, Scott Edited April 12, 2024 by shusta
Bedders Posted April 13, 2024 Posted April 13, 2024 As you're using VBS couldn't you instead use a batch file if you need to script it? @ECHO OFF NET USE O: /delete NET USE O: \\servername\foldername /Persistent:yes I'd be mapping it via Group Policy directly though, as you can opt for 'Replace' to do so. 1
thimon Posted April 13, 2024 Posted April 13, 2024 +1 for group policy mapping. Ditched scripts years ago for mapping drives.
dapaulio Posted April 14, 2024 Posted April 14, 2024 I understand you want your user to be able disconnect and reconnect two different drives mapped to O on the fly Is there a reason it has to be the same letter? Nowadays scripting is becoming more and more locked down in enterprise environments so depending on your site security scripting may not be the option. The alternative would be show them how to map the drive via explorer gui Simplest method of scripting is a batch script as mentioned above. You can adapt it so that it asks the user which profile you want it to do. Set /p choice=which drive would you like to install? If ‘%choice%’==‘1’ goto :choice1 If ‘%choice%’==‘2’ goto :choice2 :Choice1 Net use o /delete Net use O: \\server\share Goto end :choice2 Net use o /delete Net use O: \\server\share Goto end :End You can add other validation and error management in to it all also if you can be bothered
shusta Posted April 15, 2024 Author Posted April 15, 2024 Hello! Thanks for the suggestion. We have locked down our user PC's and they are not allowed to run anything in the CMD window. I know for a fact that users can run VBS scripts. This is why I was asking for help converting this to VBS. We are upgrading hardware in all locations and this means someone will need to manually touch each PC to make the changes (as simple as they are). This is the script I had for the project till I was told users no longer has this ability/access: @echo OFFNET USE O: /delete NOTE: This is the old server shareNET USE O: \\server 1\folder /Persistent:yes NOTE: this is the new server with the same share namecopy /y "\\server 1\folder\new.lnk" "%Public%\Desktop\new.lnk" NOTE: This is a new shortcut they will all need Users can float from location to location as well. This is the easiest way to manage this application and user. Technically they should only need to run this one for the new server share and shortcut but there are times when they call and we find the connection was dropped. This is to prevent extra phone calls.
Julian Posted April 16, 2024 Posted April 16, 2024 Think about a sighned powershell script. The sighning means that if the script is chanaged, it will break the sighning and not be runnable. new-smbMapping -localpath "v:" -remotepath "\\172.16.*********" -username "172.16.*******" -password "*******" # SIG # Begin signature block # MIIJAQYJKoZIhvcNAQcCoIII8jCCCO4CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUmiFPtWS67EPZ7YyQOnYwIIXs
Bedders Posted April 16, 2024 Posted April 16, 2024 Ok sorry, I misunderstood your post. Note that I haven't tested this at all but it might point you in the right direction. First we declare all of the variables we're using, for simplicity's sake. Then we ask the user to enter either 1 or 0 (also accepts new or n or old or o, in any capitalisation). More options can be added with additional CASEs. Then I nicked some code from StackOverflow on how to unmap then map a drive. More error checking could be useful here to see whether the drive has actually unmapped before mapping it. I've thrown this together because I'm bored and haven't played with VBS for a few years. PLEASE test this first Good luck! Dim Message, Title, DefaultValue, EnteredValue, ErrorMessage, DriveLetter, OldPath, NewPath Message = "Please enter '1' or 'New' for the new server, or '2' or 'Old' for the old server" ' Set prompt instructing user ErrorMessage = "Please choose either New or Old." 'Set error message text Title = "Do you want the New or Old server?" ' Set title. ErrorTitle = "ERROR: Chosen Option Not Recognised" ' Set error message title DefaultValue = "1" ' Set default. DriveLetter = "O:" 'What drive letter are we replacing. NEEDS TO END IN A COLON. NewPath = "\\FileServer\Share" 'New server path OldPath = "\\OldFIleServer\Share" ' Old server path Dim WshNetwork Set WshNetwork = WScript.CreateObject("WScript.Network") Do While X = 0 EnteredValue = InputBox(Message, Title, DefaultValue) 'Prompt the user EnteredValue = LCase(EnteredValue) 'Convert to lower case for comparison select case EnteredValue case 1, 'new', 'n' On Error Resume Next ' remove current drive mapping, force the removal, force persistent WshNetwork.RemoveNetworkDrive DriveLetter, True, True ' and wait for network drive removal (this could take some time) Wscript.Sleep 1000 Err.Clear ' map network drive, force persistent WshNetwork.MapNetworkDrive DriveLetter, NewPath, True On Error GoTo 0 Exit Do Case 2, 'old', 'o' On Error Resume Next ' remove current drive mapping, force the removal, force persistent WshNetwork.RemoveNetworkDrive DriveLetter, True, True ' and wait for network drive removal (this could take some time) Wscript.Sleep 1000 Err.Clear ' map network drive, force persistent WshNetwork.MapNetworkDrive DriveLetter, NewPath, True Exit Do case else msgBox(ErrorMessage,16,ErrorTitle) Exit Do end select End Do
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now