Link to home
Start Free TrialLog in
Avatar of smuth
smuth

asked on

VB Logon Script not working correctly

Purplepomegranite helped me with this script yesterday and it works perfectly on my machine, but when I try it on a couple of test users with GP or manually running it, they are getting the following error message:

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

*** Set time from server ***
*** Map Shared and Apps drives ***
Z:\SfldLogon.vbs(18, 1) WSHNetwork.RemoveNetworkDrive: This network connection d
oes not exist.

The user did not have any of his network connections so there was nothing there to disconnect, but I am not sure how to change the script to go to the next line and map the drive if the connection is not already there for disconnection. As I stated it is working on my machine, but it does not appear to disconnect the drives first. It looks like it is just going to the group membership section.

Thanks, Scott...
' First force the script to use the command line, so we can see Echo's
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
	strPath = Wscript.ScriptFullName
	strCommand = "%comspec% /k cscript  """ & strPath & """"
	Set objShell = CreateObject("Wscript.Shell")
	objShell.Run(strCommand), 1, True
	Wscript.Quit
End If
 
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
 
WScript.Echo "*** Set time from server ***"
strCommand = "net time \\dc1 /set /yes"
objShell.Run strCommand, 1, True
 
WScript.Echo "*** Map Shared and Apps drives ***"
objNetwork.RemoveNetworkDrive "I:", True, True
WScript.Sleep 200
objNetwork.MapNetworkDrive "I:", "\\fs3\iso", False
objNetwork.RemoveNetworkDrive "M:", True, True
WScript.Sleep 200
objNetwork.MapNetworkDrive "M:", "\\fs3\marketing", False
objNetwork.RemoveNetworkDrive "S:", True, True
WScript.Sleep 200
objNetwork.MapNetworkDrive "S:", "\\fs3\shared", False
 
WScript.Echo "*** Map Home drives ***"
objNetwork.RemoveNetworkDrive "P:", True, True
WScript.Sleep 200
objNetwork.MapNetworkDrive "P:", "\\fs1\home$\" & objNetwork.UserName, False
 
WScript.Echo "*** Map Drives as per group membership ***"
 
Set objSysInfo = CreateObject("ADSystemInfo")
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)
 
' Loop and check group membership for user
on error resume next
For Each strGroup in objUser.MemberOf
	strGroupPath = "LDAP://" & strGroup
	Set objGroup = GetObject(strGroupPath)
	strGroupName = objGroup.CN
	WScript.Echo objSysInfo.Username & ": " & strGroupName
	
	Select Case strGroupName
		Case "ops"
			WScript.Echo "*** Map Root drive ***"
			objNetwork.RemoveNetworkDrive "L:", True, True
			WScript.Sleep 200
			objNetwork.MapNetworkDrive "L:", "\\fs3\PDP", False
		Case "pdp"
			WScript.Echo "*** Map LanAdmin drive ***"
			objNetwork.RemoveNetworkDrive "O:", True, True
			WScript.Sleep 200
			objNetwork.MapNetworkDrive "O:", "\\fs3\OPS", False
		case else
			WScript.Echo objSysInfo.Username & ": " & strGroupName
	End Select
Next

Open in new window

Avatar of Shift-3
Shift-3
Flag of United States of America image

You can do a WMI query against the Win32_NetworkConnection class to determine whether the drive is mapped before unmapping it.  However, it might be simplest to just delete all network drives at the beginning.
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0915.mspx
Avatar of Chris Dent

Or a FileSystemObject.DriveExists call (see below).

Or just don't worry about it and suppress the error with "On Error Resume Next" for the MapDrive section.

I'd lose all the WScript.Sleep 200 statements, you shouldn' t need all those delays.

Chris


Set objFileSystem = CreateObject("Scripting.FileSystemObject")
 
If objFileSystem.DriveExists(strLetter) Then
  ' Remove the Drive
End If

Open in new window

Avatar of smuth
smuth

ASKER

I just added the On Error Resume Next and removed all of the delays and this seems to be working. Thank you very much. What do I need to add at the end to get the script box to disappear when it is done running?

Thanks, Scott...

Hmm actually I haven't a clue. Never used a command window in that manner with VbScript. Hopefully Shift-3 knows :) I take it a simple WScript.Quit doesn't work?

I did want to add one more comment on the script above:

WScript.Echo "*** Set time from server ***"
strCommand = "net time \\dc1 /set /yes"
objShell.Run strCommand, 1, True


If you're using AD and Windows 2000 or above clients then Net Time is a deprecated command, you really shouldn't be using it. Time synchronisation is handled by the w32tm service, you can force a w32tm /resync there if you wish, it really shouldn't be necessary though.

Chris
Avatar of smuth

ASKER

I wondered about the time sync, but as I do not know vbscipt I wasn't sure if it needed to be there for some reason. I removed it, thank you.

I had tried the WScript.Quit  at the end and this does not work. Is there another way I should be running this logon script?
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland 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
SOLUTION
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
And yes, remove all the WScript.Echo commands.

Alternatively, change the /k to /c on line 4.