?
Solved

NEED a VBScript to query users in an AD Security Group and then send an email to the users manager

Posted on 2009-03-30
13
Medium Priority
?
608 Views
Last Modified: 2012-05-06
I am needing a VBScript that query all the members a security group from active directory, and then email each of the members assigned manager.  

The subject will be the same, however the body of the email will need to have the users name at the begining and then the message will be the same for all. This way they know who the email is regarding.

So I will need to know where to specify the security group the script needs to query, and then where to customize the subject and the body of the email.
0
Comment
Question by:seaninman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 14

Expert Comment

by:rejoinder
ID: 24056784
Can you clarify something... did you want the script to loop through a security group and list the members, then for each member, an email will be sent to the manager to include a static subject line and a body which will include the users name?
0
 
LVL 4

Author Comment

by:seaninman
ID: 24059605
Exactly correct
0
 
LVL 14

Accepted Solution

by:
rejoinder earned 2000 total points
ID: 24062222
Can you try this.  It will need you to edit some of the variables so that the right group is selected and that emails are being sent through the right server etc.
I commented the areas that need your attention.
'Enter the LDAP path to the security group in question
Set objGroup = GetObject("LDAP://CN=Accountants,OU=Finance,DC=fabrikam,DC=com")
 
For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    strUserName = objUser.CN
    strManagerDN = objUser.Manager
    strUsersEmailAddress = objUser.mail
    if strManagerDN <> "" then
        Set objManager =  GetObject("LDAP://" & strManagerDN)
        strManagerEmailAddress = objManager.mail
        if strManagerEmailAddress <> "" then
            EmailManager strUserName,"your.email@domain.com",strManagerEmailAddress
        end if
    end if
Next
 
Sub EmailManager(UserName,FromField,ManagerEmailAddress)
    On Error Resume Next
    Dim objMessage
    Set objMessage = CreateObject("CDO.Message")
    with objMessage
        .From = FromField				'<--From field being pulled from sub variable
        .To = ManagerEmailAddress			'<--To field being pulled from sub variable
        .Subject = "Here is the subject line to change"	'<--Subject line for you to change
        .TextBody = "Here is the email body followed by the username '" & UserName & "' and finishes with more email body."	'<--Email body for you to change.  Note the break in the line for inserting the user name.
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver"	'<--Enter you email server name here
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        .Configuration.Fields.Update
        .Send
    end with 
    Set objMessage = Nothing
End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:seaninman
ID: 24062741
I am getting an error on Line 4
Error: Object not a collection
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24062803
Is line 2 pointing to the correct security group?
("LDAP://CN=Accountants,OU=Finance,DC=fabrikam,DC=com")
For instance it might look like this&
("LDAP://CN=Test Group,OU=Test,OU=My Groups,DC=domain,DC=com")
Also, can you peek inside the group and double check that it has members and that the membership has propagated to other DC's.
0
 
LVL 4

Author Comment

by:seaninman
ID: 24062924
Okay, so I'm not getting any errors now, but its not sending an email.
0
 
LVL 4

Author Comment

by:seaninman
ID: 24062959
Nevermind... LOL  I forgot to change the mailserver.. :)  Thanks for the help..
0
 
LVL 4

Author Closing Comment

by:seaninman
ID: 31564417
Great JOB!!!
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24062981
You are welcome - glad to help out!
0
 
LVL 4

Author Comment

by:seaninman
ID: 24063057
One more thing I could use with this script..  Is there a way that we can have it write a log file (txt) that shows who an email was sent to and for what user?
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24063279
OK same as before but this time there will be logging.  See line 1 for the file name.
Like before, please test it out and let me know if you run into any problems.
strFile     = "email-manager-log.csv"
Set objFSO  = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, 2, True)
objFile.WriteLine """DATE"",""USER"",""MANAGER"",""ACTION"""
 
'Enter the LDAP path to the security group in question
Set objGroup = GetObject("LDAP://CN=Accountants,OU=Finance,DC=fabrikam,DC=com")
 
For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    strUserName = objUser.CN
    strManagerDN = objUser.Manager
    strUsersEmailAddress = objUser.mail
    if strManagerDN <> "" then
        Set objManager =  GetObject("LDAP://" & strManagerDN)
        strManagerEmailAddress = objManager.mail
        if strManagerEmailAddress <> "" then
            EmailManager strUserName,"your.email@domain.com",strManagerEmailAddress
            objFile.WriteLine """" & now() & """,""" & strUserName & """,""" & strManagerEmailAddress & """,""Email sent"""
        end if
    else
        objFile.WriteLine """" & now() & """,""" & strUserName & ""","" "",""Manager not assigned - no email sent"""
    end if
Next
 
Sub EmailManager(UserName,FromField,ManagerEmailAddress)
    On Error Resume Next
    Dim objMessage
    Set objMessage = CreateObject("CDO.Message")
    with objMessage
        .From = FromField				'<--From field being pulled from sub variable
        .To = ManagerEmailAddress			'<--To field being pulled from sub variable
        .Subject = "Here is the subject line to change"	'<--Subject line for you to change
        .TextBody = "Here is the email body followed by the username '" & UserName & "' and finishes with more email body."	'<--Email body for you to change.  Note the break in the line for inserting the user name.
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver"	'<--Enter you email server name here
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        .Configuration.Fields.Update
        .Send
    end with 
    Set objMessage = Nothing
End Sub

Open in new window

0
 
LVL 4

Author Comment

by:seaninman
ID: 24063876
Works perfect.. THANKS
0
 
LVL 4

Author Comment

by:seaninman
ID: 24064276
Here is another one I have that you might be able to help me with...

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24293789.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question