Link to home
Start Free TrialLog in
Avatar of tshobe
tshobe

asked on

Script for disconnecting mapped drive, map home drive and another drive. If possible run group policy through script

I am new to VB Scripting.  Here is a little background.  I have around 100 laptops that won't do a prelogon to authenticate against the domain.  I am left to a local account on a machine and mapping network drives and hopefully the group policy.  My main goal is to map drives.  Here is what I am trying to accomplish.
1.  Disconnect drives in steps 3 and 4 if they exist. If not then.
2.  prompt for domain user name and password.
3.  map home drive for said username
4.  map second drive for users
5.  If this is possible pull down the group policy.

I have gotten bits and pieces from other sites to do 2, 3, and 4 somewhat, but am struggling with step 1.  Here is what I have so far.
   ' Volume to remove
  strDrive = "Y:"
 
  Set objNet = CreateObject("wscript.network")
  objNet.RemoveNetworkDrive "Y:", True, True

  ' Volume to remove
  strDrive = "Z:"
 
  Set objNet = CreateObject("wscript.network")
  objNet.RemoveNetworkDrive "Z:", True, True

  End If

Set network = WScript.CreateObject("WScript.Network")
uName = InputBox("Please enter you User Name")
uName = uName
pWord = InputBox("Please enter you Password")
homeDrive = "\\SERVER\shares\Student\users\" & uName

network.MapNetworkDrive "Y:", homeDrive,True,uName,pWord

Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
strLocalDrive = "Z:"
strRemoteShare = "\\server\shares\student\public"
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, strPer, strUsr, strPas

End If

Thank you for any help in advance.
Avatar of Batuhan Cetin
Batuhan Cetin
Flag of Türkiye image

Well, the FileSystemObject has a .DriveExists() method that would tell you if the mapping is there or not but I wouldnt bother checking to see if they are mapped already, doesnt really matter if they are or not.  Just disconnect the drives if they exist, if they dont exist the script will just move on.

Also you can set this up as a logon script to apply it through group policy. Place the vbs in your server's netlogon folder and set it as a logon script from group policy editor for your OU.

If you need further assistance please update.
Avatar of tshobe
tshobe

ASKER

I need to do an If, then statement.  I think.  Sometimes it doesn't have a drive there, and when it doesn't then the VB Script errors out.  I was thinking of
1. If drive maps
2. then remove drive
Please let me know if my thinking is not right.
Avatar of tshobe

ASKER

I forgot to say that I have group policies set up.  If I didn't have 100 laptops connecting to wireless and the wireless is inadequate on autenticating against the domain then I wouldn't have to do this.  I wish that the prelogon for the wireless would work better, but that seems to be an issue with a lot of people.  I really do appreciate your assistance.
Then let me give you two methods: First one, what I would do; second one, what you want to do.

As I told before you can do it with the DriveExists method but what I'm trying to say is usage of the method is unnecessary in your situation. If you just remove all Y and Z network drives, and map them to the share you want, there won't be any problems. This is more practical and make your code work faster when the number of clients and the number of things you want to check if exists is large.

>> Sometimes it doesn't have a drive there, and when it doesn't then the VB Script errors out.

We scripters generally put "on error resume next" to this kind of scripts which will execute in non-standard and large environments. Put this piece of code to the very beginning of your script and you'll be fine. This is one solution.

The second one, what you want to do is in the code piece below. It removes a drive if exists and maps it to a share. Please update if you need more assistance


On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objNetwork = CreateObject("Wscript.Network") 

If (objFSO.DriveExists("Y:") = True) Then 
    objNetwork.RemoveNetworkDrive "Y:", True, True 
End If 
objNetwork.MapNetworkDrive "Y:", "\\Server\Share"

Open in new window

Avatar of tshobe

ASKER

Thank you BatuhanCetin!!
I placed it in there and now it doesn't map the Y Drive when I try it again.  Here is the script.  I really appreciate your help.
' Volume to remove
  strDrive = "Y:"

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")

If (objFSO.DriveExists("Y:") = True) Then
    objNetwork.RemoveNetworkDrive "Y:", True, True
End If

Set network = WScript.CreateObject("WScript.Network")
uName = InputBox("Please enter you User Name")
uName = uName
pWord = InputBox("Please enter you Password")
homeDrive = "\\SERVER\shares\Student\users\" & uName

network.MapNetworkDrive "Y:", homeDrive,True,uName,pWord

Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
strLocalDrive = "Z:"
strRemoteShare = "\\server\shares\student\public"
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, strPer, strUsr, strPas
There are a few wrong lines. Can you try this:


On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objNetwork = CreateObject("Wscript.Network") 

If (objFSO.DriveExists("Y:") = True) Then 
    objNetwork.RemoveNetworkDrive "Y:", True, True 
End If

uName = InputBox("Please enter you User Name")
pWord = InputBox("Please enter you Password")
homeDrive = "\\SERVER\shares\Student\users\"

objNetwork.MapNetworkDrive "Y:", homeDrive,True,uName,pWord

Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
strRemoteShare = "\\server\shares\student\public"
objNetwork.MapNetworkDrive "Z:", strRemoteShare, strPer, strUsr, strPas

Open in new window

Avatar of tshobe

ASKER

I tried this and it mapped the Z drive, but it didn't map the Y drive.  I really do appreciate your help!!!!!
I tried this myself and worked. Ok let's do an error check. Remove the "On Error Resume Next" at the first line and run the script again. Please send me the error details.
Avatar of tshobe

ASKER

Here is a screenshot of the error.  It is coming up saying
line 12
char 1
the network path was not found
error 80070035

Thank you again.
Capture.JPG
Can you access "\\SERVER\shares\Student\users\" without the script? Can you try manually mapping this drive on a test client with the uName and pWord you provide in the script?
Avatar of tshobe

ASKER

I took out the \ after users and it mapped.  homeDrive = "\\SERVER\shares\Student\users".  The only problem now is that if I try it with a different user account it give me this.

Thanks again

Capture1.JPG
Can you try this:


On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objNetwork = CreateObject("Wscript.Network") 

DIM objShell
set objShell = WScript.CreateObject("WScript.shell")
o = objShell.Run("CMD /C net use /DELETE Y:", , True)
v = objShell.Run("CMD /C net use /DELETE Z:", , True)

If (objFSO.DriveExists("Y:") = True) Then 
    objNetwork.RemoveNetworkDrive "Y:", True, True 
End If

uName = InputBox("Please enter you User Name")
pWord = InputBox("Please enter you Password")
homeDrive = "\\SERVER\shares\Student\users"

objNetwork.MapNetworkDrive "Y:", homeDrive,True,uName,pWord

Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
' strLocalDrive = "Z:"
strRemoteShare = "\\server\shares\student\public"
objNetwork.MapNetworkDrive "Z:", strRemoteShare, strPer, strUsr, strPas

Open in new window

Oh by the way I copied my draft here. There is a commented line 22:

' strLocalDrive = "Z:"

please delete that line.
Avatar of tshobe

ASKER

You Rock.  I mixed yours with some that I had and it is working.  You are the best thank you!!!!!!!!!!
Here is what I ended up with.
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")

DIM objShell
set objShell = WScript.CreateObject("WScript.shell")
o = objShell.Run("CMD /C net use /DELETE Y:", , True)
v = objShell.Run("CMD /C net use /DELETE Z:", , True)

If (objFSO.DriveExists("Y:") = True) Then
    objNetwork.RemoveNetworkDrive "Y:", True, True
End If



Set network = WScript.CreateObject("WScript.Network")
uName = InputBox("Please enter you User Name")
uName = uName
pWord = InputBox("Please enter you Password")
homeDrive = "\\SERVER\shares\Student\users\" & uName

network.MapNetworkDrive "Y:", homeDrive,True,uName,pWord

'Purpose of the script is to Connect or Disconnect the Z drive
'If connected it disconnects

Set WshShell = WScript.CreateObject("WScript.Shell")
Set GuyNet = WScript.CreateObject("WScript.Network")
Set CheckDrive = GuyNet.EnumNetworkDrives()

DriveLetter = "Z:" 'DriveLetter must be a CAPITAL letter
RemotePath = "\\server\shares\student\public"

AlreadyConnected = False
'GuyNet.RemoveNetworkDrive DriveLetter
For i = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(i) = DriveLetter Then AlreadyConnected = True
Next

If AlreadyConnected = True then
GuyNet.RemoveNetworkDrive DriveLetter
GuyNet.MapNetworkDrive DriveLetter, RemotePath

Else
GuyNet.MapNetworkDrive DriveLetter, RemotePath

End if
Avatar of tshobe

ASKER

Please reply so that I can award points to you.
ASKER CERTIFIED SOLUTION
Avatar of Batuhan Cetin
Batuhan Cetin
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tshobe

ASKER

Thank you for your help and I will take your advice.  It is nice to have a clean script.
Avatar of tshobe

ASKER

This was a great first experience.