Question

Populate active directory computer description with Location manufacturer's name model number and logged on username

Asked by: o-team

Hi Guys,

I hope to find an answer to my quest here. I would appreciate any help here.

I have a spreadsheet that has all computer names and the location details ONLY example.

ComputerName  Location
Computer 1         Building A
Computer 2         Building B  and the list goes on.....

I have nearly 300 computers in our OU and would like to run a start up script (I can do that using GPO) which does mainly 3 things.

1) when the user logs on it looks for the spreadsheet and then matches the computer name and then picks up the location and change the active directory computer description to Location of the computer along with computer Model name | Model Number | Logged on User Name.

It should look something like "Building A | Dell Inc | Optiplex 650 | Name of the user"

2) I should also be able to sync this details with the local computer description if possible.

3)  I want all this to happen when a user logs on to a machine.

I have scratched my head looking for something like this but to no avail...so the max number of points for this.....



Thanks in advance,
Manzoor

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-29 at 00:16:58ID24691679
Tags

Active directory computer description manufacturer name model logon script

Topic

VB Script

Participating Experts
1
Points
500
Comments
27

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Organizing OU's
    When setting up OU's, in my case for departments- In addition to adding the departments computers, I'm adding the users. Do people usually just add the user group, or the users and the group to the OU?
  2. OU Policy
    Hi guys, How does it work we create an OU add computers in this OU but the policy linked to this OU are not being applied to the Computers....why not?? we always have to create a group and add this computer to the group before the GPO applies.... any idea?? Tenks
  3. Populating an OU
    Hi, I am new to the whole OU set-up and need to get some clarification on how I can achieve my aim. What I want to do is as follows: I have users that will use a roaming profile, I have added them to a group called "Roaming" I have created an OU called Roaming. W...
  4. GPO's
    I want to use GPO (group policies) to force passwords to be more complexed. in my windows 2003 domain....I've made a new test container and made a policy. I went into computer configuration/windows settings/security settings/account policies/password policies, I enabled abunc...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: RobSampsonPosted on 2009-08-30 at 17:30:08ID: 25219994

Hi there,

This cannot be done with a StartUp script, because no user is logged in yet, so that part won't work.

If you are able to use a Logon Script instead, this can all be achieved.  What I suggest though, is to save the Excel file as a CSV file for this script to reference, it's much easier that way.

If that sounds OK, I can help write the script for you.

Regards,

Rob.

 

by: o-teamPosted on 2009-08-31 at 00:30:51ID: 25221021

HI Rob,

My mistake...I meant to say Logon script "mistyped it as Startup script". I had found a script that works to some extent..see below link.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23552760.html?sfQueryTermInfo=1+chang+comput+descript

I can save that file as CSV, no issues. What I need it to just work no matter what the means by which it works.

User logs on...script looks for that specified file (CSV) in shared location....it then matches the computer name and copies the computer description from the CSV file to active directory and also replicates the computer description to to the local computer description.

Thanks for your help in advance,

 

by: RobSampsonPosted on 2009-09-01 at 00:04:27ID: 25229094

OK, sure, that can be done.  So you'll have a CSV file in which the contents will be

COMPUTERA,Building Department
COMPUTERB,Finance Department
etc...

That's fine. I will write some code tomorrow to do that for you.

Regards,

Rob.

 

by: o-teamPosted on 2009-09-01 at 00:50:17ID: 25229305

Thanks looking forward to it.

 

by: RobSampsonPosted on 2009-09-01 at 23:37:22ID: 25238414

For "name of the user" do you want just the login name? That's the easiest....

Rob.

 

by: o-teamPosted on 2009-09-01 at 23:41:23ID: 25238423

Yes please just the user login name would do.

Thanks,

 

by: RobSampsonPosted on 2009-09-01 at 23:57:35ID: 25238491

Hi, try this.  The local computer description update may require a reboot.  Test it by running it on one machine first.

Regards,

Rob.

strCSVFile = "\\server\share\CompDescriptions.csv"
Set objNetwork = CreateObject("WScript.Network")
strComputerName = UCase(objNetwork.ComputerName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002 
Set objDict = CreateObject("Scripting.Dictionary")
Set objCSV = objFSO.OpenTextFile(strCSVFile, intForReading, False)
While Not objCSV.AtEndOfStream
	strLine = objCSV.ReadLine
	If InStr(strLine, ",") > 0 Then
		objDict.Add UCase(Split(strLine, ",")(0)), Split(strLine, ",")(1)
	End If
Wend
objCSV.Close
If objDict.Exists(strComputerName) = True Then
	Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	strDescription = objDict(strComputerName)
	For Each objItem In colItems
		strManufacturer = objItem.Manufacturer
		strModel = objItem.Model
	Next
	strUsername = objNetwork.UserName
	' Set the Domain computer description
	Set objADSysInfo = CreateObject("ADSystemInfo")
	Set objADComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)
	objADComputer.description = strDescription & " | " & strManufacturer & " | " & strModel & " | " & strUsername
	objADComputer.SetInfo
	Set objADComputer = Nothing
	' Now set the local computer description via the srvcomment value in the Registry
	Set objRegistry = GetObject ("winmgmts:\\.\root\default:StdRegProv") 
	strKeyPath = "System\CurrentControlSet\Services\lanmanserver\parameters"
	strValueName = "srvcomment"
	objRegistry.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strDescription & " | " & strManufacturer & " | " & strModel & " | " & strUsername
	MsgBox "Your description has been updated."
Else
	MsgBox "Could not find " & strComputerName & " in the CSV file."
End If

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:

Select allOpen in new window

 

by: RobSampsonPosted on 2009-09-01 at 23:59:07ID: 25238496

Welcome to EE by the way!

 

by: o-teamPosted on 2009-09-02 at 01:38:27ID: 25238994

Hi Rob,

You are a champion. I have tested on a test machine, is there a way to get rid of the spaces. As I run the script it updates the AD computer description as designed. But has too much spaces between each segment. (both in active directory as well as the local computer description). Usually after the Model Name and Model Number ? Any ideas what changes do I need to make to make it work without those extra spaces.

Also I noticed as you said reboot is necessary or else that change for the local computer description does not work. Not sure how to resolve this as in a practical case the user might login and just shutdown the machine when they leave rather than rebooting the machine. Any ideas on this please.


Thanks,
Manzoor

 

by: RobSampsonPosted on 2009-09-02 at 01:50:13ID: 25239044

Hi, as far as the spaces go, we're only writing this to it, so I'm not sure why there's extra spacing.
objADComputer.description = strDescription & " | " & strManufacturer & " | " & strModel & " | " & strUsername


Maybe you could change that to:
objADComputer.description = Replace(Trim(strDescription) & " | " & Trim(strManufacturer) & " | " & Trim(strModel) & " | " & Trim(strUsername), "  ", " ")

and see how it goes.

As for the restart requirement, I'm not sure.  Maybe we could restart the "Server" or "Browser" services or something, but I'd have to test that tomorrow, as to which would reflect the change properly.

Regards,

Rob.

 

by: RobSampsonPosted on 2009-09-02 at 01:57:02ID: 25239067

Actually, it should be the "Server" service. There's some "robust" service restart code here:
http://techtasks.com/code/viewbookcode/280

which we could add as a function and restart the Server service for you, becuase the Computer Browser is a dependant service as well.

I can add that for you tomorrorow too.

Regards,

Rob.

 

by: o-teamPosted on 2009-09-02 at 02:03:25ID: 25239096

TRIM function did the Job. I will test it on the actual test machine tomorrow at work. Your help is greatly appreciated.

Cool that would be great if the local computer description could be updated without restart.

Thanks Rob,
Manzoor

 

by: RobSampsonPosted on 2009-09-02 at 02:38:00ID: 25239235

Actually, I've had some time to put it together now.  Give this a try.

Regards,

Rob.

strCSVFile = "\\server\share\CompDescriptions.csv"
Set objNetwork = CreateObject("WScript.Network")
strComputerName = UCase(objNetwork.ComputerName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002 
Set objDict = CreateObject("Scripting.Dictionary")
Set objCSV = objFSO.OpenTextFile(strCSVFile, intForReading, False)
While Not objCSV.AtEndOfStream
	strLine = objCSV.ReadLine
	If InStr(strLine, ",") > 0 Then
		objDict.Add UCase(Split(strLine, ",")(0)), Split(strLine, ",")(1)
	End If
Wend
objCSV.Close
If objDict.Exists(strComputerName) = True Then
	Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	strDescription = objDict(strComputerName)
	For Each objItem In colItems
		strManufacturer = objItem.Manufacturer
		strModel = objItem.Model
	Next
	strUsername = objNetwork.UserName
	' Set the Domain computer description
	Set objADSysInfo = CreateObject("ADSystemInfo")
	Set objADComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)
	objADComputer.description = Replace(Trim(strDescription) & " | " & Trim(strManufacturer) & " | " & Trim(strModel) & " | " & Trim(strUsername), "  ", " ")
	objADComputer.SetInfo
	Set objADComputer = Nothing
	' Now set the local computer description via the srvcomment value in the Registry
	Set objRegistry = GetObject ("winmgmts:\\.\root\default:StdRegProv") 
	strKeyPath = "System\CurrentControlSet\Services\lanmanserver\parameters"
	strValueName = "srvcomment"
	objRegistry.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, Replace(Trim(strDescription) & " | " & Trim(strManufacturer) & " | " & Trim(strModel) & " | " & Trim(strUsername), "  ", " ")
	RobustServiceRestart ".", "Server"
	MsgBox "Your description has been updated."
Else
	MsgBox "Could not find " & strComputerName & " in the CSV file."
End If
 
Sub RobustServiceRestart(strComputer, strService)
	' From the book "Active Directory, Third Edition"
	' ISBN: 0-596-10173-2
 
	' Stop dependent services
	Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set objServiceList = objWMI.ExecQuery("Associators of " _
		& "{Win32_Service.Name='" & strService & "'} Where " _
		& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
	For Each objService In objServiceList
		objService.StopService()
	Next
	WScript.Sleep 10000   ' Pause to allow services to stop
 
	' Stop target service
	Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
	objService.StopService()
	WScript.Sleep 10000   ' Pause to allow service to stop
 
	' Start target service
	Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
	objService.StartService()
	WScript.Sleep 10000   ' Pause to allow service to start
 
	' Start dependent services
	Set objServiceList = objWMI.ExecQuery("Associators of " _
		& "{Win32_Service.Name='" & strService & "'} Where " _
		& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
	For Each objService In objServiceList
		objService.StartService()
	Next
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:

Select allOpen in new window

 

by: o-teamPosted on 2009-09-02 at 22:02:09ID: 25248000

Hi Rob,

The latest version does not work. It gives me this error as attached.

Thanks,
Manzoor

 

by: RobSampsonPosted on 2009-09-02 at 22:16:06ID: 25248036

OK, so maybe there's no dependant service. Try this.

Regards,

Rob.

strCSVFile = "\\server\share\CompDescriptions.csv"
Set objNetwork = CreateObject("WScript.Network")
strComputerName = UCase(objNetwork.ComputerName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002 
Set objDict = CreateObject("Scripting.Dictionary")
Set objCSV = objFSO.OpenTextFile(strCSVFile, intForReading, False)
While Not objCSV.AtEndOfStream
	strLine = objCSV.ReadLine
	If InStr(strLine, ",") > 0 Then
		objDict.Add UCase(Split(strLine, ",")(0)), Split(strLine, ",")(1)
	End If
Wend
objCSV.Close
If objDict.Exists(strComputerName) = True Then
	Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	strDescription = objDict(strComputerName)
	For Each objItem In colItems
		strManufacturer = objItem.Manufacturer
		strModel = objItem.Model
	Next
	strUsername = objNetwork.UserName
	' Set the Domain computer description
	Set objADSysInfo = CreateObject("ADSystemInfo")
	Set objADComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)
	objADComputer.description = Replace(Trim(strDescription) & " | " & Trim(strManufacturer) & " | " & Trim(strModel) & " | " & Trim(strUsername), "  ", " ")
	objADComputer.SetInfo
	Set objADComputer = Nothing
	' Now set the local computer description via the srvcomment value in the Registry
	Set objRegistry = GetObject ("winmgmts:\\.\root\default:StdRegProv") 
	strKeyPath = "System\CurrentControlSet\Services\lanmanserver\parameters"
	strValueName = "srvcomment"
	objRegistry.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, Replace(Trim(strDescription) & " | " & Trim(strManufacturer) & " | " & Trim(strModel) & " | " & Trim(strUsername), "  ", " ")
	RobustServiceRestart ".", "Server"
	MsgBox "Your description has been updated."
Else
	MsgBox "Could not find " & strComputerName & " in the CSV file."
End If
 
Sub RobustServiceRestart(strComputer, strService)
	' From the book "Active Directory, Third Edition"
	' ISBN: 0-596-10173-2
 
	' Stop dependent services
	Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set objServiceList = objWMI.ExecQuery("Associators of " _
		& "{Win32_Service.Name='" & strService & "'} Where " _
		& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
	On Error Resume Next
	For Each objService In objServiceList
		objService.StopService()
	Next
	If Err.Number = 0 Then WScript.Sleep 10000   ' Pause to allow services to stop
	Err.Clear
 
	' Stop target service
	Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
	objService.StopService()
	WScript.Sleep 10000   ' Pause to allow service to stop
 	' Start target service
	objService.StartService()
	WScript.Sleep 10000   ' Pause to allow service to start
 
	' Start dependent services
	Set objServiceList = objWMI.ExecQuery("Associators of " _
		& "{Win32_Service.Name='" & strService & "'} Where " _
		& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
	On Error Resume Next
	For Each objService In objServiceList
		objService.StartService()
	Next
	Err.Clear
	On Error GoTo 0
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:

Select allOpen in new window

 

by: o-teamPosted on 2009-09-09 at 02:30:21ID: 25289452

Hi Rob,

I am doing some tests on this and will let you know if that worked.

Thanks,

 

by: RobSampsonPosted on 2009-09-09 at 15:59:09ID: 25296292

No problem. I haven't had a chance to test it, so let me know how it goes.

Regards,

Rob.

 

by: o-teamPosted on 2009-10-13 at 01:20:40ID: 25557888

Hi Rob,

I will try that code this weekend and see if it works.

Thanks,
Manzoor

 

by: RobSampsonPosted on 2009-10-20 at 23:52:43ID: 25621247

Hi there, did you manage to try this out?

Regards,

Rob.

 

by: o-teamPosted on 2009-10-28 at 01:00:18ID: 25680745

HI Rob,

Sorry for the delay. I am currently swept away in setting up an exchange server so had no chance to test it out.

I will definitely try this out on tuesday when I will have enough time to test it as its a public holiday here. I will surely get back to you by Wednesday with the results.

As always thanks for your help.

Cheers,
Manzoor

 

by: RobSampsonPosted on 2009-10-28 at 19:55:54ID: 25690311

No problem. We've got the Melbourne Cup public holiday here on Tuesday.  Should be good weather too!

Rob.

 

by: o-teamPosted on 2009-11-02 at 20:08:36ID: 25725980

Hi Rob,

Had a chance to test it on a dummy machine. It gives this error.

 

by: RobSampsonPosted on 2009-11-03 at 14:05:43ID: 25734313

Hi, well that would suggest to me that you have two values in the first column of the CSV file that are equal (case insensitive).  Can you make sure you don't have any duplicates? In any case, I have changed the code to let you know when it finds one of these duplicates, so that it should at least continue working.

Regards,

Rob.

strCSVFile = "\\server\share\CompDescriptions.csv"
Set objNetwork = CreateObject("WScript.Network")
strComputerName = UCase(objNetwork.ComputerName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const HKEY_LOCAL_MACHINE = &H80000002 
Set objDict = CreateObject("Scripting.Dictionary")
Set objCSV = objFSO.OpenTextFile(strCSVFile, intForReading, False)
While Not objCSV.AtEndOfStream
	strLine = objCSV.ReadLine
	If InStr(strLine, ",") > 0 Then
		If objDict.Exists(UCase(Split(strLine, ",")(0))) = False Then
			objDict.Add UCase(Split(strLine, ",")(0)), Split(strLine, ",")(1)
		Else
			MsgBox UCase(Split(strLine, ",")(0)) & " already exists in the CSV. Please check that it is not a duplicate."
		End If
	End If
Wend
objCSV.Close
If objDict.Exists(strComputerName) = True Then
	Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT Manufacturer,Model FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	strDescription = objDict(strComputerName)
	For Each objItem In colItems
		strManufacturer = objItem.Manufacturer
		strModel = objItem.Model
	Next
	strUsername = objNetwork.UserName
	' Set the Domain computer description
	Set objADSysInfo = CreateObject("ADSystemInfo")
	Set objADComputer = GetObject("LDAP://" & objADSysInfo.ComputerName)
	objADComputer.description = Replace(Trim(strDescription) & " | " & Trim(strManufacturer) & " | " & Trim(strModel) & " | " & Trim(strUsername), "  ", " ")
	objADComputer.SetInfo
	Set objADComputer = Nothing
	' Now set the local computer description via the srvcomment value in the Registry
	Set objRegistry = GetObject ("winmgmts:\\.\root\default:StdRegProv") 
	strKeyPath = "System\CurrentControlSet\Services\lanmanserver\parameters"
	strValueName = "srvcomment"
	objRegistry.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, Replace(Trim(strDescription) & " | " & Trim(strManufacturer) & " | " & Trim(strModel) & " | " & Trim(strUsername), "  ", " ")
	RobustServiceRestart ".", "Server"
	MsgBox "Your description has been updated."
Else
	MsgBox "Could not find " & strComputerName & " in the CSV file."
End If
 
Sub RobustServiceRestart(strComputer, strService)
	' From the book "Active Directory, Third Edition"
	' ISBN: 0-596-10173-2
 
	' Stop dependent services
	Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set objServiceList = objWMI.ExecQuery("Associators of " _
		& "{Win32_Service.Name='" & strService & "'} Where " _
		& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
	On Error Resume Next
	For Each objService In objServiceList
		objService.StopService()
	Next
	If Err.Number = 0 Then WScript.Sleep 10000   ' Pause to allow services to stop
	Err.Clear
 
	' Stop target service
	Set objService = objWMI.Get("Win32_Service.Name='" & strService & "'")
	objService.StopService()
	WScript.Sleep 10000   ' Pause to allow service to stop
 	' Start target service
	objService.StartService()
	WScript.Sleep 10000   ' Pause to allow service to start
 
	' Start dependent services
	Set objServiceList = objWMI.ExecQuery("Associators of " _
		& "{Win32_Service.Name='" & strService & "'} Where " _
		& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
	On Error Resume Next
	For Each objService In objServiceList
		objService.StartService()
	Next
	Err.Clear
	On Error GoTo 0
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:

Select allOpen in new window

 

by: o-teamPosted on 2009-11-03 at 20:23:50ID: 25736394

Hi Rob,

For some reason, the text in the first column had some weird character as of which that script didn't run. I ran it again and it seems to work.

Thanks for your help. What i encountered is that only user who has admin rights are able to change the description for both active directory and Local computer description.

Any ideas how can this be made to run without giving users local admin rights.

Cheers,
Manzoor

 

by: RobSampsonPosted on 2009-11-03 at 22:13:12ID: 25736837

Hmmm, for the local setting, you could use a Group Policy Registry Permission to set Users permission to write to
HKLM\System\CurrentControlSet\Services\lanmanserver\parameters\srvcomment

As for the domain one, from here:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Windows_2003_Active_Directory/Q_23018507.html

"You need to give "Authenticated Users" permission to "Write Description" on whatever OU your Computers are in."

Regards,

Rob.

 

by: o-teamPosted on 2009-11-03 at 22:50:42ID: 25737002

Thanks Rob,

I think I will give that a go. I should be alright now.

Cheers,
Manzoor

 

by: RobSampsonPosted on 2009-11-04 at 13:55:31ID: 25744388

No worries. Thanks for the grade.

Regards,

Rob.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...