Solved

VBS Login Script with HTA

Posted on 2013-01-02
5
450 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now