Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with script logic

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
I'm trying to create a script that will notify me whenever a user is added or removed form the domain admins group. I got this to notify me when a new user is added to the group, but can't figure out how to be notified when someone is removed.

Thanks.

Code:
'==========================================================================
' 
' NAME:
' 
' AUTHOR:
' EMAIL:
'
' COMMENT:
'
' VERSION HISTORY:
' 1.0   xx/xx/xxxx  Initial release
'
'==========================================================================
Option Explicit

'==========================================================================
' If TestMode is set to true, all wscript.echo messages will be displayed,
' if set to False no messages are displayed
'==========================================================================
TestMode = True

'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objFSO, objFile0, objFile1, objFile2, objFile3, strAdmins, strTemp
Dim objGroup, arrMemberOf, strMember, objUserS, objFile, strCurrent
Dim strNewAdmins, objEmail, TestMode, WshShell, WshNetwork

Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
Set objGroup = GetObject("LDAP://cn=domain admins,ou=groups,dc=company,dc=com")
objGroup.GetInfo
 
arrMemberOf = objGroup.GetEx("member")

If Not objFSO.FileExists("C:\da_temp.txt") Then
objFSO.CreateTextFile("C:\da_temp.txt")
End If

Set objFile0 = objFSO.OpenTextFile("C:\da_temp.txt",2, True)

For Each strMember in arrMemberOf
Set objUserS = GetObject("LDAP://" & strMember)
objUserS.GetInfo
objFile0.WriteLine(objUserS.Get("displayName"))
Next

objFile0.Close

If Not objFSO.FileExists("C:\da_current.txt") Then
objFSO.MoveFile "C:\da_temp.txt", "C:\da_current.txt"
WScript.Quit
End If

Set objFile1 = objFSO.OpenTextFile("C:\da_current.txt", 1)

strCurrent = objFile1.ReadAll
objFile1.Close

Set objFile2 = objFSO.OpenTextFile("C:\da_temp.txt", 1)

Do Until objFile2.AtEndOfStream
    strTemp = objFile2.ReadLine
    If InStr(strCurrent, strTemp) <= 0 Then
        strNewAdmins = strNewAdmins & strTemp & vbCrLf
    End If
Loop

objFile2.Close

If Not objFSO.FileExists("C:\da_difference.txt") Then
objFSO.CreateTextFile("C:\da_difference.txt")
End If
Set objFile3 = objFSO.OpenTextFile("C:\da_difference.txt", 2)

objFile3.WriteLine strNewAdmins

objFile3.Close

If strNewAdmins = "" Then
WScript.Echo "No New names in the domain admins group: "
Else
Wscript.Echo "New names in the domain admins group: " & vbCrLf & strNewAdmins

End If

objFSO.MoveFile "C:\da_current.txt" , "C:\da_current_deleteme.txt"
objFSO.MoveFile "C:\da_temp.txt" , "C:\da_current.txt"
objFSO.DeleteFile "C:\da_Difference.txt"
objFSO.DeleteFile "C:\da_current_deleteme.txt"

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "gmagerr@company.com"
objEmail.To = "gmagerr@company.com"
objEmail.Subject = "THIS IS A TEST"

If strNewAdmins = "" Then
objEmail.TextBody = objEmail.TextBody & ("THIS IS A TEST") & vbCrLf 
objEmail.TextBody = objEmail.TextBody & ("No New Names in the Domain Admins Group") & vbCrLf
Else 
objEmail.TextBody = objEmail.TextBody & ("New Names in the Domain Admins Group") & vbCrLf
End If
objEmail.TextBody = objEmail.TextBody & strNewAdmins & vbCrLf

objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "mail.company.com" 
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
 
Ok, so if a user was added then they will be in the new file but not in the old. That is what this code checks:

Do Until objFile2.AtEndOfStream
strTemp = objFile2.ReadLine
If InStr(strCurrent, strTemp) <= 0 Then
strNewAdmins = strNewAdmins & strTemp & vbCrLf
End If
Loop

To find out if they were removed, just do the same thing only look for names that are in the old file but not in the new one.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
That's where I'm getting confused. I can't figure out how to do it. Maybe I just need a little break.
 
The first thing I would suggest after your break is to consider your file and object names. They are not particularly clear. Is objFile2 the old user names or the current user names? strCurrent is clearly the surrent names. If I were doing this, then I would put the contents of both files into strings (strOldFileContents and strNewFileContents). Then do something like this:

For Each strNewName In Split(strNewFileContents, vbCrLf)
If Instr(strOldFileContents, strNewName) <= 0 then
'This New name does not exist in the Old names so it is a new user
End If
Next

For Each strOldName In Split(strOldFileContents, vbCrLf)
If Instr(strNewFileContents, strOldName) <= 0 Then
'This Old name does not exist in the New names so it is a removed user
End If
Next

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
hmmm i tried what you suggested but had no luch. Can you show me using my code?

thanks.
 
To be honest with you, no. How about you show me what you tried that did not work and explain in what way it did not work. Then I will help you work it out.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
OK, that's cool. I'll figure it out. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top