Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for iechgpwd.vbs

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

  1. Option Explicit
  2.  
  3. Dim arrPasswords, strUserName
  4.  
  5. strUserName  = "JohnDoe"
  6. arrPasswords = IEChangePwd( strUserName )
  7. WScript.Echo "Change password for  : " & strUserName     & vbCrLf _
  8.            & "The old password was : " & arrPasswords(0) & vbCrLf _
  9.            & "The new password is  : " & arrPasswords(1)
  10.  
  11. Function IEChangePwd( myUserName )
  12. ' This function uses Internet Explorer to create a login dialog.
  13. ' It won't close until all fields have valid values and the OK
  14. ' button is clicked.
  15. '
  16. ' Version:             2.11
  17. ' Last modified:       2013-11-07
  18. '
  19. ' Arguments:  [string] user name
  20. ' Returns:    [array]  the old (0) and new (1) passwords
  21. '
  22. ' Written by Rob van der Woude
  23. ' http://www.robvanderwoude.com
  24. ' Error handling code written by Denis St-Pierre
  25. 	Dim arrPwd, blnValid, objIE
  26. 	blnValid = False
  27. 	' Create an IE object
  28. 	Set objIE = CreateObject( "InternetExplorer.Application" )
  29. 	' specify some of the IE window's settings
  30. 	objIE.Navigate "about:blank"
  31. 	objIE.Document.title = "Change password" & String( 80, "." )
  32. 	objIE.ToolBar        = False
  33. 	objIE.Resizable      = False
  34. 	objIE.StatusBar      = False
  35. 	objIE.Width          = 400
  36. 	objIE.Height         = 240
  37. 	' Center the dialog window on the screen
  38. 	With objIE.Document.parentWindow.screen
  39. 		objIE.Left = (.availWidth  - objIE.Width ) \ 2
  40. 		objIE.Top  = (.availheight - objIE.Height) \ 2
  41. 	End With
  42. 	' Wait till IE is ready
  43. 	Do While objIE.Busy
  44. 		WScript.Sleep 200
  45. 	Loop
  46. 	' Insert the HTML code to prompt for user input
  47. 	objIE.Document.body.innerHTML = "<div align=""center""><table cellspacing=""5"">" _
  48. 	                              & "<tr nowrap><th colspan=""2"">Change password for " _
  49. 	                              & myUserName & ":</th></tr><tr nowrap><td>Old password:" _
  50. 	                              & "</td><td><input type=""password"" size=""20"" id=" _
  51. 	                              & """OldPassword""></td></tr><tr nowrap><td>New password:" _
  52. 	                              & "</td><td><input type=""password"" size=""20"" id=" _
  53. 	                              & """NewPassword""></td></tr><tr nowrap><td>Confirm " _
  54. 	                              & "password:</td><td><input type=""password"" size=""20"" " _
  55. 	                              & "id=""ConfirmPassword""></td></tr></table>" _
  56. 	                              & "<p><input type=""hidden"" id=""OK"" name=""OK"" " _
  57. 	                              & "value=""0""><input type=""submit"" value="" OK "" " _
  58. 	                              & "onclick=""VBScript:OK.value=1""></p></div>"
  59. 	' Hide the scrollbars
  60. 	objIE.Document.body.style.overflow = "auto"
  61. 	' Make the window visible
  62. 	objIE.Visible = True
  63. 	' Set focus on password input field
  64. 	objIE.Document.all.OldPassword.focus
  65.  
  66. 	' Wait for valid input (2 non-empty equal passwords)
  67. 	Do Until blnValid = True
  68. 		' Wait till the OK button has been clicked
  69. 		On Error Resume Next
  70. 		Do While objIE.Document.all.OK.value = 0
  71. 			WScript.Sleep 200
  72. 			' Error handling code by Denis St-Pierre
  73. 			If Err Then
  74. 				IEChangePwd = Array( "", "" )
  75. 				objIE.Quit
  76. 				Set objIE = Nothing
  77. 				Exit Function
  78. 			End If
  79. 		Loop
  80. 		On Error Goto 0
  81. 		' Read the user input from the dialog window
  82. 		arrPwd = Array( objIE.Document.all.OldPassword.value, _
  83. 		                objIE.Document.all.NewPassword.value, _
  84. 		                objIE.Document.all.ConfirmPassword.value )
  85. 		' Check if the new password and confirmed password match
  86. 		If arrPwd(1) = arrPwd(2) Then
  87. 			' Check if the new password isn't empty
  88. 			If Trim( arrPwd(1) ) = "" Then
  89. 				MsgBox "The new password cannot be empty", _
  90. 				       vbOKOnly + vbInformation + vbApplicationModal, _
  91. 				       "Type new password"
  92. 				objIE.Document.all.NewPassword.value     = ""
  93. 				objIE.Document.all.ConfirmPassword.value = ""
  94. 				objIE.Document.all.OK.value              = 0
  95. 		                objIE.Document.all.NewPassword.focus
  96. 			Else
  97. 				blnValid = True
  98. 			End If
  99. 		Else
  100. 			MsgBox "The new and confirmed passwords do not match.", _
  101. 			       vbOKOnly + vbInformation + vbApplicationModal, _
  102. 			       "Retype new password"
  103. 			objIE.Document.all.NewPassword.value     = ""
  104. 			objIE.Document.all.ConfirmPassword.value = ""
  105. 			objIE.Document.all.OK.value              = 0
  106. 	                objIE.Document.all.NewPassword.focus
  107. 		End If
  108. 	Loop
  109. 	' Close and release the object
  110. 	objIE.Quit
  111. 	Set objIE = Nothing
  112. 	' Return the passwords in an array
  113. 	IEChangePwd = Array( arrPwd(0), arrPwd(1) )
  114. End Function
  115.  

page last uploaded: 2017-08-21, 14:26