Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for pwdnoexp.vbs

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

  1. Option Explicit
  2.  
  3. Dim colItems, objItem, objSysInfo, objWMISvc
  4. Dim objPasswordExpirationFlag, objUser, objUserFlags
  5. Dim strComputer, strDomainOrWorkgroup, strUser
  6.  
  7. Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
  8.  
  9. ' Check command line argument(s)
  10. With WScript.Arguments
  11. 	If .Named.Count    > 0 Then Syntax
  12. 	If .Unnamed.Count <> 1 Then Syntax
  13. 	strUser = .Unnamed(0)
  14. End With
  15.  
  16. ' Retrieve workgroup or domain name
  17. Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
  18. Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
  19. For Each objItem in colItems
  20. 	strDomainOrWorkgroup = objItem.Domain
  21. Next
  22.  
  23. ' Retrieve computer name
  24. Set objSysInfo = CreateObject( "WinNTSystemInfo" )
  25. strComputer = objSysInfo.ComputerName
  26.  
  27. ' Get the user account object
  28. Set objUser = GetObject( "WinNT://" & strDomainOrWorkgroup & "/" & strComputer & "/" & strUser & ",User" )
  29.  
  30. ' Read the user account "flags" (settings)
  31. objUserFlags = objUser.Get( "UserFlags" )
  32. ' Change a single bit in the user account flags
  33. objPasswordExpirationFlag = objUserFlags OR ADS_UF_DONT_EXPIRE_PASSWD
  34. ' Change the user account flags
  35. objUser.Put "userFlags", objPasswordExpirationFlag
  36. ' Commit the changes
  37. objUser.SetInfo
  38.  
  39. ' Release the objects
  40. Set objUser    = Nothing
  41. Set objSysInfo = Nothing
  42. Set colItems   = Nothing
  43. Set objWMISvc  = Nothing
  44.  
  45.  
  46. Function Capitalize( myString, myChars )
  47. 	' This function capitalizes a string, in this case the script name,
  48. 	' so the on screen help will display the proper capitalization
  49. 	' even if PwdNoExp.vbs is renamed to PasswordNeverExpires.vbs
  50. 	Dim arrChars, i, intStart, strResult
  51. 	If myChars <> "" Then
  52. 		strResult = LCase( myString )
  53. 		arrChars  = Split( myChars, "," )
  54. 		intStart  = 1
  55. 		For i = 0 To UBound( arrChars )
  56. 			intStart  = InStr( intStart + 1, strResult, arrChars(i), vbBinaryCompare )
  57. 			strResult = Replace( strResult, LCase( arrChars(i) ), arrChars(i), intStart + 1, 1, vbBinaryCompare )
  58. 		Next
  59. 	Else
  60. 		strResult = UCase( Left( myString ) ) _
  61. 		          & LCase( Mid(  myString ) )
  62. 	End If
  63. 	Capitalize = strResult
  64. End Function
  65.  
  66.  
  67. Sub Syntax
  68. 	Dim strMsg
  69. 	strMsg = strMsg & vbCrLf _
  70. 	       & Capitalize( WScript.ScriptName, "P,N,Ex" ) _
  71. 	       & ",  Version 1.00" & vbCrLf _
  72. 	       & "Set password expiration date to NEVER for a local user ID" _
  73. 	       & vbCrLf & vbCrLf _
  74. 	       & "Usage:  [ CSCRIPT ]  " & UCase( WScript.ScriptName ) & "  user_id" _
  75. 	       & vbCrLf & vbCrLf _
  76. 	       & "Where:  " & Chr(34) & "user_id" & Chr(34) _
  77. 	       & "   is the local user whose password should never expire" _
  78. 	       & vbCrLf & vbCrLf _
  79. 	       & "Based on a Microsoft TechNet Script Center sample:" & vbCrLf _
  80. 	       & "www.microsoft.com/technet/scriptcenter/scripts" _
  81. 	       & "/ds/local/users/lousvb14.mspx" _
  82. 	       & vbCrLf & vbCrLf _
  83. 	       & "Written by Rob van der Woude" & vbCrLf _
  84. 	       & "http://www.robvanderwoude.com"
  85. 	WScript.Echo strMsg
  86. 	WScript.Quit(1)
  87. End Sub
  88.  

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