Archive for July 2008

 
 

Automatic Password Expiration Email

This script will go through the OU you specify and email the users that their password will expire.


  1. Dim oConnection ‘As ADODB.Connection
  2. Dim oRecordSet ‘As ADODB.RecordSet
  3. Dim strQuery ‘As String
  4. Dim strDomainNC ‘As String
  5. Dim oRootDSE ‘As IADs
  6. Dim oDirObject ‘As Variant
  7. Dim vArgs, x
  8. Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
  9. Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D
  10.  
  11. On Error Resume Next
  12.  
  13. ‘ Find the domain naming context
  14. set oRootDSE = GetObject("LDAP://RootDSE")
  15. strDomainNC = oRootDSE.Get("defaultNamingContext")
  16. set oRootDSE = Nothing
  17.  
  18. ‘ Setup the ADO connection
  19. Set oConnection = CreateObject("ADODB.Connection")
  20. oConnection.Provider = "ADsDSOObject"
  21. oConnection.Open "ADs Provider"
  22. Set vArgs = WScript.Arguments
  23.  
  24. if VArgs.Count <> 2 Then
  25.    wscript.echo "USAGE: cscript expirepassemail.vbs  "
  26.    wscript.echo "USAGE: cscript expirepassemail.vbs 110 120"
  27.    wscript.quit
  28. end if
  29.  
  30. min = vArgs(0)
  31. max = vArgs(1)
  32.  
  33. Set oCommand = CreateObject("ADODB.Command")
  34. Set oCommand.ActiveConnection = oConnection
  35. oCommand.CommandText= "," &amp; strDomainNC &amp; ">;(objectCategory=user);distinguishedName,cn,name;subTree"
  36. oCommand.Properties("searchscope") = 2
  37. oCommand.Properties("Page Size") = 1000
  38. oCommand.Properties("Timeout") = 15
  39. Set oRecordSet = oCommand.Execute
  40.  
  41. if oRecordSet.Eof then
  42.   response.write "No objects were found"
  43.   WScript.Quit(0)
  44. Else
  45.   Dim vClasses ‘As Variant
  46.   Dim strClass ‘As String
  47.   Dim mysid ‘As variant
  48.  
  49.   wscript.echo "List of users and password information"
  50.   wscript.echo "           Max: " &amp; max &amp; ", Min: " &amp; min
  51.   wscript.echo "————————————–"
  52.  
  53. ‘  Iterate through the objects that are in the query results
  54.  
  55.    While Not oRecordset.Eof
  56.       Set usr = GetObject("LDAP://" &amp; oRecordset.Fields("distinguishedName").Value)
  57.       If (instr(usr.SamAccountName, "$") = 0) and instr(usr.adspath, ".Global") = 0 Then
  58.          Err.Number = 0
  59.          dtmValue = Usr.PasswordLastChanged
  60.          If Err.Number <> E_ADS_PROPERTY_NOT_FOUND Then
  61.             flags = usr.get("userAccountControl")
  62.             If flags And ADS_UF_DONT_EXPIRE_PASSWD Then
  63.                expire = "noexpire"
  64.             Else
  65.                expire = "Expires"
  66.             End if
  67.             If (int(DateDiff("d", dtmValue, Date)) > int(min)) and (int(DateDiff("d", dtmValue, Date)) < int(max)) Then
  68.                wscript.echo usr.cn &amp; " " &amp; expire &amp; " in " &amp; 120DateDiff("d", dtmValue, Date) &amp; " days"
  69.                if 120DateDiff("d", dtmValue, Date) < 0 then
  70.                   wscript.echo "      Expired! " &amp; 120DateDiff("d", dtmValue, Date)
  71.                   Set objMessage = CreateObject("CDO.Message")
  72.                   objMessage.Subject = usr.cn &amp; " Expired"
  73.                   objMessage.Sender = "DoNotReply "
  74.                   objMessage.From = "Do Not Reply"
  75.                   objMessage.To = ""
  76.                   objMessage.TextBody = usr.samaccountname &amp; " has already expired."
  77.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  78.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ""
  79.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  80.                   objMessage.Configuration.Fields.Update
  81.                   ‘ objMessage.Send
  82.                else
  83.                   response.write 120DateDiff("d", dtmValue, Date)
  84.                   Set objMessage = CreateObject("CDO.Message")
  85.                   objMessage.Subject = "Your network and email password is expiring"
  86.                   objMessage.Sender = "DoNotReply "
  87.                   objMessage.From = "Do Not Reply"
  88.                   objMessage.To = usr.samaccountname &amp; "@"
  89.                   objMessage.TextBody = usr.samaccountname &amp; "@ : Your password will expire in " &amp; 120DateDiff("d", dtmValue, Date) &amp; " days!  Please change it to avoid disruptions."
  90.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  91.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ""
  92.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  93.                   objMessage.Configuration.Fields.Update
  94.                   objMessage.Send
  95.                end if
  96.             End If
  97.          End If
  98.       End If
  99.     oRecordset.MoveNext
  100.     Wend
  101. End If
  102.  
  103. ‘Clean up
  104. Set oRecordset = Nothing
  105. Set oConnection = Nothing

Removing all the Groups from a User

This script is normally used by passing the argument of the username by executing cscript script.vbs <username> but the username could easily be hardcoded.

Set vArgs = WScript.Arguments

UserName = vArgs(0)

Const ADS_PROPERTY_DELETE = 4
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D

strUserPath = "LDAP://" & Username
Set objUser = GetObject(strUserPath)
arrMemberOf = objUser.GetEx("memberOf")

For Each Group in arrMemberOf
Set objGroup = GetObject("LDAP://" & Group)
objGroup.PutEx ADS_PROPERTY_DELETE, "member", Array(UserName)
objGroup.SetInfo
Next

Mapping a Drive with VBScript

So I recently wrote a very long and complex login script to replace the login scripts we currently have here and figured I would share certain parts of it here incase anyone else stumbles upon it and learns something.

Define these variables for both commands

Set objNetwork = CreateObject("Wscript.Network")
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")


This function was written to map and F drive to a specific server and share.
Sub MapFDrive()

'Checks to see if they have the drive mapped, if so then it deletes it.
If objFileSys.DriveExists("F:") = True Then
objNetwork.RemoveNetworkDrive "F:", True, True
End If

'Maps the Drive.
objNetwork.MapNetworkDrive "F:", "\\Server\Share"
'Gives the drive a name so it's not just <share name> on <server> (Drive:).
objShell.nameSpace("F:\").Self.name = "Sharename can be whatever you want."

End Sub


And this one, which is a little more complex, allows for dynamic mapping via the script
Sub MapDrive(strFileServerPush, strDriveLetter, strMapPath, strDriveName)

'Checks to see if they have the drive mapped, if so then it deletes it.
If objFileSys.DriveExists(strDriveLetter) = True Then
objNetwork.RemoveNetworkDrive strDriveLetter, True, True
End If

'Constructs the path to map the drive.
strPath = "\\" & strFileServerPush & "\" & strMapPath
'Maps the Drive according to the path above.
objNetwork.MapNetworkDrive strDriveLetter, strPath
'Gives the drive a name so it's not just <share name> on <server> (Drive:).
objShell.nameSpace(strDriveLetter & "\").Self.name = strDriveName

End Sub


The above function is called like so:

MapDrive “<Server Name>”, “<Drive Letter>”, “<Share Name>”, “<Whatever you want to name it>”