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.
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
'==========================================================================