Solved

VBS Login Script with HTA

Posted on 2013-01-02
5
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

615 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