Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

VBScript Help: It runs to quickly before detecting the network and Active Directory.

Hello,

I need a line added in the VBScript that will delay setting environment variables for 10 seconds or so.  It appears it doesn't work well when using Wifi.  There is sometype of delay.  What would be nice is to verify Active Directory is available then run the script.  That might be the most helpful.  If doing this in Powershell would be better, let me know.

Thanks!

Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

' This will create the variable %mail% for the primary mail address
objUserEnv("MAIL") = objUser.mail
' This will create the variable %FirstName% for First Name of User
objUserEnv("FirstName") = objUser.givenName
' This will create the variable %LastName% for the surname of User
objUserEnv("LastName") = objUser.sn

Open in new window

Avatar of Jeremy Weisinger
Jeremy Weisinger

WScript.Sleep 10000 should add a 10 second delay.
Avatar of mystikal1000

ASKER

Yea I tried that, but it still didn't work.

Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
WScript.Sleep 10000 'sleep for 10 seconds

Open in new window

Your delay needs to be before you try and connect to AD. My VBScript skills are a little dusty but perhaps something like this:

hostname = "8.8.8.8" 'IP or name of domain controller
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping=1
Do Until Ping=0
    wscript.echo "sleeping"
    Ping = WshShell.Run("ping -n 1 " & hostname, 0, True) 
    wscript.sleep 1000
Loop 

Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

' This will create the variable %mail% for the primary mail address
objUserEnv("MAIL") = objUser.mail
' This will create the variable %FirstName% for First Name of User
objUserEnv("FirstName") = objUser.givenName
' This will create the variable %LastName% for the surname of User
objUserEnv("LastName") = objUser.sn

Open in new window

Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

wscript.sleep 10000

' This will create the variable %mail% for the primary mail address
objUserEnv("MAIL") = objUser.mail
' This will create the variable %FirstName% for First Name of User
objUserEnv("FirstName") = objUser.givenName
' This will create the variable %LastName% for the surname of User
objUserEnv("LastName") = objUser.sn

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

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
This will wait until the AD user object is actually retrieved, or give up after a timeout:
bConnected = false
iTimeout = 300
iSeconds = 0
iPause = 5
Set objADSysInfo = CreateObject("ADSystemInfo")
On Error Resume Next
While (Not bConnected) and (iSeconds < iTimeout)
	Err.Clear
	Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
	If Err.Number = 0 Then
		bConnected = true
	Else
		WScript.Echo "Waiting for AD (0x" & Hex(Err.Number) & ")" & Err.Source & Err.Description
		WScript.Sleep (iPause * 1000)
		iSeconds = iSeconds + iPause
	End If
Wend
Err.Clear
On Error Goto 0
If Not bConnected Then
	WScript.Echo "Still unable to connect to AD after " & iTimeout & " seconds, giving up."
	WScript.Quit 1
End If

WScript.Echo "Connected: " & objUser.givenName & " " & objUser.sn
Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")

' This will create the variable %mail% for the primary mail address
objUserEnv("MAIL") = objUser.mail
' This will create the variable %FirstName% for First Name of User
objUserEnv("FirstName") = objUser.givenName
' This will create the variable %LastName% for the surname of User
objUserEnv("LastName") = objUser.sn

Open in new window

Thanks Guys!
Glad to help. :)
Ah. Misread question. Good job