Support Forums

Help with VB Script - Get the Managers sAmAccountName?

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.
User avatar
Maries
Posts: 17
Joined: Wed Jan 21, 2015 10:23 am

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby Maries » Fri Jan 23, 2015 3:57 pm

Thank you all, I apologize for the bad post!

The script that I have is part of a SQL job. The job runs the script and I didn't realize that the entire script wasn't posted.

I'm going to update the post, I was trying to edit it, but I don't see that option.

I am new to VB and also SQL jobs that run scripts, so I appreciate the help.
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby jvierra » Sat Jan 24, 2015 11:32 am

How to use a direct AD query as a linked server in MSSQLServer.

This is the T-SQL script that sets up your linked server. Using this you can query and update tables using a simple task trigger. This is more secure and much faster than using the very old VBScript methods.

Code: Select all

--Create a Linked Server
EXEC sp_addlinkedserver @server = 'ADSI', @srvproduct = 'Active Directory Services 2.5', @provider = 'ADSDSOObject', @datasrc = 'adsdatasource'

--Configure the server to allow OPENQUERY functions
sp_configure 'show advanced options', 1
reconfigure with override

sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure

#EXEC sp_addlinkedsrvlogin @rmtsrvname = 'ADSI', @useself = 'true'

SELECT * FROM OpenQuery(
ADSI, 'SELECT * FROM ''LDAP://DC=<your domain>,DC=<your domain>'' WHERE objectCategory=''User''')
User avatar
Maries
Posts: 17
Joined: Wed Jan 21, 2015 10:23 am

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby Maries » Wed Jan 28, 2015 8:58 pm

Thank you again! Here are the details:

1- This script runs within a SQL job on a 2008 sql server.
2- It retrieves AD data and populates a SQL DB with that data
3- It functions fine using the entire code below, this was written by a SQL DBA a few years ago and I was able to add a couple user attribs to it.
4- Now the request is to add the sAMAccountName of each users Manager to the list of items populated in the database. I am not sure how to retrieve that as I can get the Managers DN, but that's easy as I have found.

the code is below, any help would be very appreciated
User avatar
Maries
Posts: 17
Joined: Wed Jan 21, 2015 10:23 am

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby Maries » Wed Jan 28, 2015 9:03 pm

I couldn't get the code to paste as the forum kept prompting there were to many off-site URLs for new users..I tried removing everything I could find, but no luck, so I attached it as a text doc.

Thx!
Marie
Attachments
sqljob_script.txt
(6.59 KiB) Downloaded 187 times
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby jvierra » Wed Jan 28, 2015 9:10 pm

I showed you how to get the manager. I don't understabd what you are asking for now.

just use the code to get the manager.
User avatar
Maries
Posts: 17
Joined: Wed Jan 21, 2015 10:23 am

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby Maries » Mon Feb 02, 2015 10:43 am

I added the code to the script, but the manager doesn't get added to the list of attributes populated in the database.

Can you tell me where in the script the code has to go and also I assume I need to add in a line that adds the name to the sql record set?
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby jvierra » Mon Feb 02, 2015 1:47 pm

The managers field is likley not used on most objects. Have you looked in AD to see if it has been set.

I do not see in your code where you have assigned the manager.
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby jvierra » Mon Feb 02, 2015 1:51 pm

In your code you would do this:

VBScript Code
Double-click the code block to select all.
Set mgr = GetObject("LDAP://" & rs.Fields("manager") )
msgbox mgr.SamAccountName
User avatar
Maries
Posts: 17
Joined: Wed Jan 21, 2015 10:23 am

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby Maries » Mon Feb 02, 2015 2:34 pm

I should have been clearer. When I run that code you provided, it does gather the managers samaccountname, but I have no idea where I would add that code to the script I posted in order for the managers samaccountname to get added to the list of attributes that are populated to the database.

The message box isn't going to help me, this is supposed to run as a scheduled job that involves no interaction.

Thanks for the help though, I'll do something else.
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Help with VB Script - Get the Managers sAmAccountName?

Postby jvierra » Mon Feb 02, 2015 2:48 pm

So you are saying you don't know how to do simple assignment in VBScript?

Just assing the field to the results of the mgr.SAmaccountName

rs.Field("manager") = mgr.SamAccountName

Return to “VBScript”

Who is online

Users browsing this forum: No registered users and 7 guests