Solved

VBS Login Script with HTA

Posted on 2013-01-02
5
447 Views
Last Modified: 2013-01-05
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?
0
Comment
Question by:winstalla
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 38738474
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
 

Author Comment

by:winstalla
ID: 38739348
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 38742039
Hi, you can use
            MapDrive "G:", "\\server\users\" & objNetwork.Username

Regards,

Rob.
0
 

Author Closing Comment

by:winstalla
ID: 38743110
That's it! I obviously didn't put that in correctly! So that's more down to my poor spelling!
Thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38746527
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now