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.
tshobeAsked:
Who is Participating?
 
Batuhan CetinCommented:
Glad you solved it :) Just for recommendation on scripting, don't do double assignments like:

uName = InputBox("Please enter you User Name")
uName = uName

The second line definitely does not do anything, you already assign the variable on the first line.

If the problem is solved please close the question by accepting the solution :)
0
 
Batuhan CetinCommented:
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.
0
 
tshobeAuthor Commented:
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.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
tshobeAuthor Commented:
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.
0
 
Batuhan CetinCommented:
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

0
 
tshobeAuthor Commented:
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
0
 
Batuhan CetinCommented:
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

0
 
tshobeAuthor Commented:
I tried this and it mapped the Z drive, but it didn't map the Y drive.  I really do appreciate your help!!!!!
0
 
Batuhan CetinCommented:
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.
0
 
tshobeAuthor Commented:
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
0
 
Batuhan CetinCommented:
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?
0
 
tshobeAuthor Commented:
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
0
 
Batuhan CetinCommented:
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

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

' strLocalDrive = "Z:"

please delete that line.
0
 
tshobeAuthor Commented:
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
0
 
tshobeAuthor Commented:
Please reply so that I can award points to you.
0
 
tshobeAuthor Commented:
Thank you for your help and I will take your advice.  It is nice to have a clean script.
0
 
tshobeAuthor Commented:
This was a great first experience.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.