Can someone make this PowerShell? :) It works great as it is but CDO is such a drag. Enjoy!
‘Script to list all password that will list
‘all accounts with passwords expiring within the next 30 days
On Error Resume Next
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D
Const ONE_HUNDRED_NANOSECOND = .000000100
Const SECONDS_IN_DAY = 86400
DayCount = 30
OUtoSearch = “OU=NY”
Set OFSO = CreateObject(“Scripting.FileSystemObject”)
nof = “E:\logs\AcctsToExp.txt”
If OFSO.FileExists (nof) Then
OFSO.DeleteFile(“E:\logs\AcctsToExp.txt”)
End If
If not OFSO.FileExists (nof) Then
Set objFile = OFSO.CreateTextFile(nof)
End If
‘Const ForAppending = 8
‘Set objFile = OFSO.OpenTextFile (nof, ForAppending)
objFile.WriteBlankLines (1)
objFile.WriteLine “Passwords due to expire in the” & ” ” & OUtoSearch & ” OU” & ” during the next ” & DayCount & ” Days” & vbTab & vbTab & Now
objFile.WriteLine
Set objConnection = CreateObject(“ADODB.Connection”)
objConnection.Open “Provider=ADsDSOObject;”
Set objCommand = CreateObject(“ADODB.Command”)
objCommand.ActiveConnection = objConnection
objCommand.CommandText = “
“userAccountControl,distinguishedName,sAMAccountName,cn;subtree”
‘objCommand.CommandText = “
‘”userAccountControl,distinguishedName,sAMAccountName,cn;subtree”
‘-objCommand.CommandText = “
‘- “;userAccountControl,distinguishedName,sAMAccountName;subtree”
objCommand.Properties(“Page Size”) = 4000
objCommand.Properties(“Timeout”) = 90
objCommand.Properties(“Cache Results”) = True
Set objRecordSet = objCommand.Execute
intCounter = 0
‘wscript.echo objRecordset.RecordCount
If objRecordset.RecordCount > 0 then
objRecordset.MoveFirst
end if
Do Until objRecordset.EOF
Uname = objRecordset.Fields(“distinguishedName”)
ShName = objRecordset.Fields(“sAMAccountName”)
Namelenght = Len(ShName)
SpacesToAdd = 15 – NameLenght
DisplayName = ShName & Space(SpacesToAdd)
Set objUser = GetObject(“LDAP://” & Uname )
intUserAccountControl = objUser.Get(“userAccountControl”)
If intUserAccountControl And ADS_UF_DONT_EXPIRE_PASSWD Then
‘objFile.WriteLine DisplayName & vbTab & vbTab & “The password does not expire.”
Else
dtmValue = objUser.PasswordLastChanged
intTimeInterval = Int(Now – dtmValue)
Set objDomain = GetObject(“LDAP://DC=myco,DC=com”)
Set objMaxPwdAge = objDomain.Get(“maxPwdAge”)
If objMaxPwdAge.LowPart = 0 Then
objFile.WriteLine DisplayName & vbTab & vbTab & “The Maximum Password Age is set to 0, Password does not expire.”
Else
dblMaxPwdNano = Abs(objMaxPwdAge.HighPart * 2^32 + objMaxPwdAge.LowPart)
dblMaxPwdSecs = dblMaxPwdNano * ONE_HUNDRED_NANOSECOND
dblMaxPwdDays = Int(dblMaxPwdSecs / SECONDS_IN_DAY)
End If
If intTimeInterval >= dblMaxPwdDays Then
objFile.WriteLine DisplayName & vbTab & vbTab & “The password has expired.”
Else
If Int((dtmValue + dblMaxPwdDays) – Now) < DayCount Then
objFile.WriteLine DisplayName & vbTab & vbTab & "The password will expire on " & DateValue(dtmValue + dblMaxPwdDays) & " (" & Int((dtmValue + dblMaxPwdDays) - Now) & " days from today)."
End If
End If
End If
intCounter = intCounter + 1
objRecordset.MoveNext
Loop
objConnection.Close
objfile.close
OFSO.Close
'====================================================
'EMAIL Routine
'====================================================
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Password Expiration Report For New York Office"
objMessage.From = "PASSWORD-EXPIRATION@myco.com"
objMessage.To = "SupportSupervisors@myco.com"
objMessage.TextBody = "Password expiration for New York Attached"
objMessage.AddAttachment "e:\logs\acctstoexp.txt"
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.myco.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
Recent Comments