Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ielogin.vbs

(view source code of ielogin.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim arrLogin, strName
  4. strName = Null
  5. If WScript.Arguments.Unnamed.Count = 1 Then strName = WScript.Arguments.Unnamed(0)
  6. arrLogin = IELogin( strName )
  7. WScript.Echo arrLogin(0) & vbTab & arrLogin(1)
  8.  
  9.  
  10. Function IELogin( myName )
  11. ' This function uses Internet Explorer to create a login dialog.
  12. '
  13. ' Script Name:          IELogin.vbs
  14. ' Version:              4.00
  15. ' Last modified:        2016-12-22
  16. '
  17. ' Arguments:  [string]  optional user name (use "" or Null to leave user name field blank)
  18. ' Returns:    [array]   the user name (0) and password (1) typed in the dialog screen
  19. '
  20. ' The output of IELogin.vbs is meant to be used in a batch file, using the following
  21. ' batch code or something similar (note: the white space following delims= is a tab):
  22. '
  23. ' REM * * * start of batch code * * *
  24. '
  25. ' FOR /F "tokens=1,2 delims=	" %%A IN ('CSCRIPT //NoLogo IELogin.vbs') DO (
  26. '     SET Name=%%~A
  27. '     SET Password=%%~B
  28. ' )
  29. ' ECHO The password of %Name% is %Password%
  30. '
  31. ' REM * * * end of batch code * * *
  32. '
  33. ' Written by Rob van der Woude
  34. ' http://www.robvanderwoude.com
  35. ' Error handling code written by Denis St-Pierre
  36.  
  37. 	Dim intScreenHeight, intScreenWidth
  38. 	Dim colItems, objIE, objItem, objWMIService, wshShell
  39. 	Dim strHTML, strDialogTitle, strName
  40.  
  41. 	'On Error Resume Next
  42.  
  43. 	strName = Trim( " " & myName )
  44.  
  45. 	' Create an IE object
  46. 	Set objIE = CreateObject( "InternetExplorer.Application" )
  47. 	' specify some of the IE window's settings
  48. 	objIE.Navigate "about:blank"
  49. 	strDialogTitle       = "Login" & String( 80, Chr( 8 ) )
  50. 	objIE.Document.Title = strDialogTitle
  51. 	objIE.ToolBar        = False
  52. 	objIE.Resizable      = False
  53. 	objIE.StatusBar      = False
  54. 	objIE.Width          = 320
  55. 	objIE.Height         = 180
  56. 	' Wait till IE is ready
  57. 	Do While objIE.Busy
  58. 		WScript.Sleep 200
  59. 	Loop
  60. 	' Center the dialog window on the screen
  61. 	Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
  62. 	Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_DesktopMonitor" )
  63. 	For Each objItem in colItems
  64. 		intScreenHeight = objItem.ScreenHeight
  65. 		intScreenWidth  = objItem.ScreenWidth
  66. 	Next
  67. 	objIE.Left = ( intScreenWidth  - objIE.Width  ) \ 2
  68. 	objIE.Top  = ( intScreenHeight - objIE.Height ) \ 2
  69. 	' Insert the HTML code to prompt for user input
  70. 	strHTML = "<div style=""font-family: sans-serif; text-align: center; margin-left: auto; margin-right: auto;"">\n" _
  71. 	        & "<table>\n" _
  72. 	        & "<tr>\n" _
  73. 	        & "\t<td colspan=""3"">&nbsp;</td>\n" _
  74. 	        & "<tr>\n" _
  75. 	        & "\t<td>Name:</td>\n" _
  76. 	        & "\t<td>&nbsp;</td>\n" _
  77. 	        & "\t<td><input type=""text"" size=""20"" autocomplete=""off"" id=""LoginName"" value=""" & strName _
  78. 	        & """ onkeyup=""javascript:if(event.keyCode==13){document.getElementById('Password').focus();}"" /></td>\n" _
  79. 	        & "</tr>\n" _
  80. 	        & "<tr>\n" _
  81. 	        & "\t<td>Password:</td>\n" _
  82. 	        & "\t<td>&nbsp;</td>\n" _
  83. 	        & "\t<td><input type=""password"" size=""21"" id=""Password"" " _
  84. 	        & "onkeyup=""javascript:if(event.keyCode==13){document.getElementById('OK').value=1;}"" /></td>\n" _
  85. 	        & "</tr>\n" _
  86. 	        & "</table>\n" _
  87. 	        & "<p><input type=""hidden"" id=""OK"" name=""OK"" value=""0"" />" _
  88. 	        & "<input type=""button"" value="" OK "" onclick=""VBScript:OK.Value=1""></p>\n" _
  89. 	        & "</div>"
  90. 	objIE.Document.Body.InnerHTML = Replace( Replace( strHTML, "\t", vbTab ), "\n", vbCrLf )
  91. 	' Hide the scrollbars
  92. 	objIE.Document.Body.Style.overflow = "auto"
  93. 	' Make the window visible
  94. 	objIE.Visible = True
  95. 	' Set focus on the appropriate input field
  96. 	If strName = "" Then
  97. 		objIE.Document.All.LoginName.Focus
  98. 	Else
  99. 		objIE.Document.All.Password.Focus
  100. 	End If
  101.  
  102. 	' Wait till the OK button has been clicked
  103. 	Do While objIE.Document.All.OK.Value = 0 
  104. 		WScript.Sleep 200
  105. 		' Error handling, by Denis St-Pierre
  106. 		If Err Then	' User clicked red X (or alt-F4) to close IE window
  107. 			IELogin = Array( "", "" )
  108. 			objIE.Quit
  109. 			Set objIE = Nothing
  110. 			Exit Function
  111. 		End if
  112. 	Loop
  113.  
  114. 	' Make the dialog active
  115. 	Set wshShell = CreateObject( "WScript.Shell" )
  116. 	wshShell.AppActivate strDialogTitle
  117. 	Set wshShell = Nothing
  118.  
  119. 	' Read the user input from the dialog window
  120. 	IELogin = Array( objIE.Document.All.LoginName.Value, objIE.Document.All.Password.Value )
  121. 	' Close and release the object
  122. 	objIE.Quit
  123. 	Set objIE = Nothing
  124.  
  125. 	On Error Goto 0
  126. End Function
  127.  

page last uploaded: 2017-07-06, 12:37