Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBS Login Script with HTA

Posted on 2013-01-02
5
Medium Priority
?
476 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

877 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