VBS Login Script with HTA

Sorry for asking a dumb question, but I've been trying to get this working all day and, whilst there are lots of ways that don't work the way I want it to, I know that there must be a simple way of getting it to work. Surely!

All I want (and why is it that the simplest requests cause so much trouble?) is a basic Logon script that brings up a HTA window (without a title bar, cos some moron will only close it down otherwise!) that says hello (by name and computer) lists what the script is doing, maps a handful of basic drives (one is individual to each User), and closes down.

The HTA would look like this (use your imagination! :) )
Hello, [Username]
You are connected to [Computer Name]
Mapping Network Drives. Please wait...
Mapping G: to [Shared Drive]
Mapping H: to [Individual User Folder]
Checking any additional network drives (based on AD Groups)
Logon complete.

Any help? Please?
winstallaAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Hi, you can use
            MapDrive "G:", "\\server\users\" & objNetwork.Username

Regards,

Rob.
0
 
RobSampsonCommented:
Hi, here's a starting example....you can add more drives in the Window_OnLoad procedure.

Regards,

Rob.

<head>
<title>Login Script</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Login Script"
     BORDER="dialog"
     SCROLL="no"
     SHOWINTASKBAR="no"
     SINGLEINSTANCE="yes"
     SYSMENU="no"
     WINDOWSTATE="normal"
>
</head>

<script language="VBScript">

	Const HKEY_CURRENT_USER = &H80000001
	Const wbemFlagReturnImmediately = &h10
	Const wbemFlagForwardOnly = &h20
	
	Dim iTimerID
	
	Sub Window_OnLoad
		intWidth = 800
		intHeight = 650
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
		
		Set objNetwork = CreateObject("WScript.Network")
		Set objADSysInfo = CreateObject("ADSystemInfo")
		Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
		ShowOutput "<br>Hello " & objUser.DisplayName
		ShowOutput "<br>You are logged onto " & objNetwork.ComputerName
		ShowOutput "<br><br>Please wait while your drives are mapped..."
		MapDrive "G:", "\\server\share"
		HTASleep 1
		MapDrive "H:", "\\server\share2"
		HTASleep 1
		ShowOutput "<br><br>Script complete"
		HTASleep 3
		iTimerID = window.setInterval("CloseWindow", 3000)
	End Sub

	Sub CloseWindow
		HTASleep 1
		window.close
	End Sub
		
	Sub ShowOutput(strMessage)
		txt_policy.innerHTML = txt_policy.innerHTML & strMessage
	End Sub

	Sub MapDrive(strDriveLetter, strDrivePath)
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objNetwork = CreateObject("WScript.Network")
		ShowOutput "<br>Mapping " & strDriveLetter & " to " & strDrivePath & "....."
		On Error Resume Next
		If objFSO.DriveExists(strDriveLetter) = True Then objNetwork.RemoveNetworkDrive strDriveLetter, True, True
		objNetwork.MapNetworkDrive strDriveLetter, strDrivePath, True
		If Err.Number = 0 Then
			ShowOutput "<font color='green'>Success</font>"
		Else
			ShowOutput "<font color='red'>Failed</font>"
		End If
		Err.Clear
		On Error GoTo 0
	End Sub

	Sub HTASleep(intSeconds)
		Set objShell = CreateObject("WScript.Shell")
		objShell.Run "ping 127.0.0.1 -n " & intSeconds + 1, 0, True
	End Sub
</script>

<body>

<table align="center" width="100%" border="0">
	<tr valign="center" align="center">
		<td colspan="2" valign="center" align="center" style="font-family: arial; font-size: 20px; font-weight: bold;"><h2>Login Script</h2></td>
	</tr>
	<tr valign="center" align="center">
		<td colspan="2" valign="center" align="center"><span id="txt_policy"></span></td>
	</tr>
</table>

</body>

Open in new window

0
 
winstallaAuthor Commented:
Thank you, Rob,
There is only one issue that I have found so far; I cannot manage to get a drive mapping to \\server\users\{individual folder}. Is there a way around this?

I have tried: %username%, objUser and UserName, all without success.

Otherwise, this is precisely the kind of thing that I was looking for!
0
 
winstallaAuthor Commented:
That's it! I obviously didn't put that in correctly! So that's more down to my poor spelling!
Thank you!
0
 
RobSampsonCommented:
No problem.  You may have had the quotes in the wrong spot.  Quite a few people do that.

Glad to help.  Thanks for the grade.

Regards,

Rob.
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.