Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-12-16
18
Medium Priority
?
527 Views
Last Modified: 2012-05-08
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.
0
Comment
Question by:tshobe
  • 10
  • 8
18 Comments
 
LVL 11

Expert Comment

by:Batuhan Cetin
ID: 26060546
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
 

Author Comment

by:tshobe
ID: 26067054
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
 

Author Comment

by:tshobe
ID: 26067075
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Batuhan Cetin
ID: 26069611
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
 

Author Comment

by:tshobe
ID: 26069706
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
 
LVL 11

Expert Comment

by:Batuhan Cetin
ID: 26069780
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
 

Author Comment

by:tshobe
ID: 26069833
I tried this and it mapped the Z drive, but it didn't map the Y drive.  I really do appreciate your help!!!!!
0
 
LVL 11

Expert Comment

by:Batuhan Cetin
ID: 26069837
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
 

Author Comment

by:tshobe
ID: 26069861
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
 
LVL 11

Expert Comment

by:Batuhan Cetin
ID: 26069873
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
 

Author Comment

by:tshobe
ID: 26069894
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
 
LVL 11

Expert Comment

by:Batuhan Cetin
ID: 26069925
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
 
LVL 11

Expert Comment

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

' strLocalDrive = "Z:"

please delete that line.
0
 

Author Comment

by:tshobe
ID: 26070051
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
 

Author Comment

by:tshobe
ID: 26070062
Please reply so that I can award points to you.
0
 
LVL 11

Accepted Solution

by:
Batuhan Cetin earned 2000 total points
ID: 26070109
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
 

Author Comment

by:tshobe
ID: 26070140
Thank you for your help and I will take your advice.  It is nice to have a clean script.
0
 

Author Closing Comment

by:tshobe
ID: 31666646
This was a great first experience.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question