We help IT Professionals succeed at work.

Is there a way to enter a timeout in a VB logon script?  ex. no drive mapped - end script after 30 seconds?

I am using a logon script to remove and map network drives. I am not running the logon script via GP. I set the users logon script manually in ADUC.

How would I script a timeout so that if the network drives are not mapped in 30 seconds the script ends?

Here is a copy of the current script that I'm running.


'ON ERROR RESUME NEXT 'ignore errors

'Clean up old drive mappings

Dim objNetwork, objFSO, objShell, objExec
Dim bForce, strCommand
Dim strDriveLetter, arrDrives, strDriveMap, arrMappings
bForce = True
arrDrives = Array("P:", "G:")

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Section which removes strDriveLetter
For Each strDriveLetter In arrDrives
      If objFSO.DriveExists(strDriveLetter) = True Then
            strCommand = "net use " & strDriveLetter & " /DELETE"
            'MsgBox strCommand
            Set objExec = objShell.Exec(strCommand)
            While objExec.Status = 0
                  ' Wait for the command to finish
            Set objExec = Nothing
     End If
strDriveLetter1 = "P:"
strDriveLetter2 = "G:"  
strRemotePath1 = "\\CHSBOFPS01\Groups\Charleston Shares\drawings"
strRemotePath2 = "\\CHSBOFPS01\Groups"

Set objNetwork = CreateObject("WScript.Network")

' Section which maps two drives, P: and G:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

' add a message box
'WScript.Echo "Map drives " & strDriveLetter1 & " & " & strDriveLetter2

Watch Question

I would not use Net use and just do a proper drive mapping vbscript.  Here is example code which will map your H: drive to something like a home direcotry.  If the drive mapping fails, it will continue to next line.

Option Explicit
On Error Resume Next
Dim objFSO, objNetwork
Dim strDriveLetter, strUNCPath, strUserName
set objFSO = CreateObject("Scripting.FileSystemObject")
set objNetwork = CreateObject("WScript.Network")
' Try map the home directory, don't do anything if H: is already mapped
strUserName = objNetwork.UserName
strDriveLetter = "H:"
strUNCPath = "\\Server\Share\" & strUserName
Sub subMappingRoutine
if NOT objFSO.FolderExists(strDriveLetter & "\") Then objNetwork.MapNetworkDrive strDriveLetter, strUNCPath, False
End Sub

Open in new window

WSH doesn't have a value for timing out when connecting a drive.  the only thing i can think of is instead using a shell.exec function (like you are already using to unmap drives) and having the script monitor the (now) external process and kill it if it goes past a threshold.


Wylkell, how would I modify the script to monitor the time and kill the script if it passes a certin threshold?

You'd want to swap out the lines where you're mapping WSH network connections with something akin to the below snippet.  I took your other exec code and modified it to put a time check into it.  i haven't tested the code so i have no idea if it's syntaxually correct but you can play with it and see if it works for you.  I'd personally make it a function long term and just reference it as a one liner like you currently do with .MapNetworkDrive
	strUNCPath = "\\something\somewhere\"
	startTime = now()
	intTimeoutThreshold = "30"
	strCommand = "net use " & strDriveLetter & "  " & strUNCPath
	Set objExec = objShell.Exec(strCommand)
	While objExec.Status = 0
		intTimeout = datediff(N,startTime,now())
		if intTimeout < intTimeoutThreshold
		end if
	Set objExec = Nothing

Open in new window

Oh, line 9 needs to be changed to:
if intTimeout > intTimeoutThreshold

cant believe i missed that, hah hah