Option Explicit
Dim arrLogin, strName
strName = Null
If WScript.Arguments.Unnamed.Count = 1 Then strName = WScript.Arguments.Unnamed(0)
arrLogin = IELogin( strName )
WScript.Echo arrLogin(0) & vbTab & arrLogin(1)
Function IELogin( myName )
' This function uses Internet Explorer to create a login dialog.
'
' Script Name: IELogin.vbs
' Version: 4.00
' Last modified: 2016-12-22
'
' Arguments: [string] optional user name (use "" or Null to leave user name field blank)
' Returns: [array] the user name (0) and password (1) typed in the dialog screen
'
' The output of IELogin.vbs is meant to be used in a batch file, using the following
' batch code or something similar (note: the white space following delims= is a tab):
'
' REM * * * start of batch code * * *
'
' FOR /F "tokens=1,2 delims= " %%A IN ('CSCRIPT //NoLogo IELogin.vbs') DO (
' SET Name=%%~A
' SET Password=%%~B
' )
' ECHO The password of %Name% is %Password%
'
' REM * * * end of batch code * * *
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Error handling code written by Denis St-Pierre
Dim intScreenHeight, intScreenWidth
Dim colItems, objIE, objItem, objWMIService, wshShell
Dim strHTML, strDialogTitle, strName
'On Error Resume Next
strName = Trim( " " & myName )
' Create an IE object
Set objIE = CreateObject( "InternetExplorer.Application" )
' specify some of the IE window's settings
objIE.Navigate "about:blank"
strDialogTitle = "Login" & String( 80, Chr( 8 ) )
objIE.Document.Title = strDialogTitle
objIE.ToolBar = False
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 320
objIE.Height = 180
' Wait till IE is ready
Do While objIE.Busy
WScript.Sleep 200
Loop
' Center the dialog window on the screen
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_DesktopMonitor" )
For Each objItem in colItems
intScreenHeight = objItem.ScreenHeight
intScreenWidth = objItem.ScreenWidth
Next
objIE.Left = ( intScreenWidth - objIE.Width ) \ 2
objIE.Top = ( intScreenHeight - objIE.Height ) \ 2
' Insert the HTML code to prompt for user input
strHTML = "<div style=""font-family: sans-serif; text-align: center; margin-left: auto; margin-right: auto;"">\n" _
& "<table>\n" _
& "<tr>\n" _
& "\t<td colspan=""3""> </td>\n" _
& "<tr>\n" _
& "\t<td>Name:</td>\n" _
& "\t<td> </td>\n" _
& "\t<td><input type=""text"" size=""20"" autocomplete=""off"" id=""LoginName"" value=""" & strName _
& """ onkeyup=""javascript:if(event.keyCode==13){document.getElementById('Password').focus();}"" /></td>\n" _
& "</tr>\n" _
& "<tr>\n" _
& "\t<td>Password:</td>\n" _
& "\t<td> </td>\n" _
& "\t<td><input type=""password"" size=""21"" id=""Password"" " _
& "onkeyup=""javascript:if(event.keyCode==13){document.getElementById('OK').value=1;}"" /></td>\n" _
& "</tr>\n" _
& "</table>\n" _
& "<p><input type=""hidden"" id=""OK"" name=""OK"" value=""0"" />" _
& "<input type=""button"" value="" OK "" onclick=""VBScript:OK.Value=1""></p>\n" _
& "</div>"
objIE.Document.Body.InnerHTML = Replace( Replace( strHTML, "\t", vbTab ), "\n", vbCrLf )
' Hide the scrollbars
objIE.Document.Body.Style.overflow = "auto"
' Make the window visible
objIE.Visible = True
' Set focus on the appropriate input field
If strName = "" Then
objIE.Document.All.LoginName.Focus
Else
objIE.Document.All.Password.Focus
End If
' Wait till the OK button has been clicked
Do While objIE.Document.All.OK.Value = 0
WScript.Sleep 200
' Error handling, by Denis St-Pierre
If Err Then ' User clicked red X (or alt-F4) to close IE window
IELogin = Array( "", "" )
objIE.Quit
Set objIE = Nothing
Exit Function
End if
Loop
' Make the dialog active
Set wshShell = CreateObject( "WScript.Shell" )
wshShell.AppActivate strDialogTitle
Set wshShell = Nothing
' Read the user input from the dialog window
IELogin = Array( objIE.Document.All.LoginName.Value, objIE.Document.All.Password.Value )
' Close and release the object
objIE.Quit
Set objIE = Nothing
On Error Goto 0
End Function