Tuesday, May 12, 2009

VBScript to Find Users with Password Set to Never Expire

The script below will search the current directory or one specified by the user and search for all of the accounts that have the attribute "Password Never Expires" set to true. The results are placed in a CSV file in the directory that the script is run from.


Set WshShell = WScript.CreateObject("WScript.Shell")
strVer = "Ver 1.0 "
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set oFile = FileSystem.CreateTextFile("PWDNeverExpires.csv", true)

strDomain = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
strUserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")
strOS = WshShell.ExpandEnvironmentStrings("%OS%")

strMessage = strMessage & "Hit Cancel to quit"
strTitle = "Domain to Search"

'Get Domain Name
UserDomain = InputBox(strMessage, strTitle, strDomain)
strMessage = ""
strTitle = ""

strMessage = "This may take a few minutes. . ."
WshShell.Popup strMessage,2,"One moment please. . . "
strMessage = ""

Set objDomain = GetObject("WinNT://" & UserDomain)
objDomain.Filter = Array("User")

For Each objUser In objDomain

  'Attempt to bind to the user
  Set UserName = GetObject("WinNT://"& UserDomain &"/"& objUser.Name &",User")

  'Check password attribute
  objPwdExpires = UserName.Get("UserFlags")
  If (objPwdExpires And &H10000) <> 0 Then
    objPwdExpiresTrue = "Yes"
    strPwdExpires = "Date Set: "
    msgPwdExpires = "Password Set to Never Expire: "
  Else objPwdExpiresTrue = "No"
    strPwdExpires = "Password Expires: "
    msgPwdExpires = "Password Set to Never Expire: "
  End If

  oFile.WriteLine (UserName.fullname & "," & UserName.name & "," & msgPwdExpires &  objPwdExpiresTrue & "," & strPwdExpires & objUser.PasswordExpirationDate)

  Set UserName = Nothing
Next
Wscript.Echo "Password Check Complete"

1 comments:

Jamie said...

Very nice script.

Worked perfect!