Jump to content

Script to Change Computer Description


Recommended Posts

I have a project where i have a list of computers with the following info: service tag, funding source. I need to create a script that runs with a group policy to query the machine for its service tag then using an if, then statement look up the service tag in the list and if it exist change the computer description to the funding source code. For example, computer A with service tag# 7LK*** is on the list and has funding source 3550 then I need to modify the computer description to RES-3550. I found the following code that gets the local service tag# (c:\wmic bios get serialnumber >) and the registry key that needs to be modified ([HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters]"srvcomment"="RES-3550") but don't know how to link the two. Please help.

Link to comment
Share on other sites


It's pretty straightfoward, but we need a little more infos.

using an if, then statement look up the service tag in the list

What list? Where is it located (read from a network share perhaps)? In what format is it? CSV or whatever? Detailed infos required here: column order, formatting, is there a header row? etc. Ideally you'd provide us with a sample.

Getting the service tag is trivial (no need for wmic, vbscript has everything needed built-in -- in fact, ~95% of this whole thing is a simple copy/paste job, along with some minor changes to glue the parts together). Setting the description in the registry is also trivial, however, don't you want to set the description in active directory as well?

Link to comment
Share on other sites

Thanks for your quick response. The list is an excel spreadsheet but it can be saved in what ever format you think is better to work with. I saved it as a csv file with 2 columns (1st column header is serial, 2nd column header is resource) I tried to upload a sample but i wasn't permitted so i uploaded the funding source code list and a text file example of the spreadsheet. The file will be saved on \\do-staff-srv\techsupport$

I originally wanted the resource code to be changed in the description field so i could query it using SCCM but it would be nice to have the info in Active Directory as well. Hopefully it isn't to much more work. Also, is this code that i would call or include in our login script so that as machines are added to the csv file they can receive these changes upon user login?

I also found this vb code to get the service tag. Hope it helps


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
("Select * from Win32_BIOS")
For each objBIOS in colBIOS
WScript.StdOut.WriteLine objBIOS.SerialNumber
Next

Funding Source Codes_Example.txt

Servicetag_ResourceCode_example.txt

Edited by gunsmokingman
Added Code Tags For Format Purpose
Link to comment
Share on other sites

Here try this code and see what it does


Dim StrComputer :StrComputer = "."
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & StrComputer & "\root\cimv2")
'-> First Check To See If This File Exists
If Fso.FileExists("Funding_Source_Codes_Example.txt") Then
Dim Ch1, Ch2, Obj, Ts
For Each Obj In Wmi.ExecQuery("SELECT * FROM Win32_BIOS")
Ch1 = Obj.SerialNumber
Next
Set Ts = Fso.OpenTextFile("Funding_Source_Codes_Example.txt",1)
Do Until Ts.AtEndOfStream
Ch2 = Ts.ReadLine
If InStr(1,Ch2,Ch1,1) Then
MsgBox "Match Found" & vbCrLf & Ch1,4128,"Confirm Match"
End If
Loop
Else
MsgBox "Missing This File Can Not Be Found" & vbCrLf & _
Fso.GetAbsolutePathName( "Funding_Source_Codes_Example.txt") ,4128,"Error No File"
End If

Link to comment
Share on other sites

@ gunsmokingman. thanks for the code. Sorry i'm not good with scripting but as i read through it i think i may have added some confusion as to what i needed. I modified and uploaded the only important file which includes 2 items (service tag# in column1 and resource code in column2). What i need the script to do is the following

*query the service tag of the local machine

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colBIOS = objWMIService.ExecQuery _

("Select * from Win32_BIOS")

For each objBIOS in colBIOS

WScript.StdOut.WriteLine objBIOS.SerialNumber

Next

*check the servicetag_resourcedoe file and see if it is listed in column1

I'm not sure how to scipt this but the file would be located on \\do-staff-srv\techsupport$

*if it is listed in column 1, then i need it to change the computer description and add the resource code in column2 (if possible add the description in AD)

the registry key that needs to be modified is ([HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters]" srvcomment"="RES-3550")

*if it is not on the list then it should just exit the script with no error message

Thanks. I appreciate any further help you can provide.

Servicetag_ResourceCode_example.txt

Edited by segujl
Link to comment
Share on other sites

@gunsmokingman. I updated my post to include the information i have and the help i need to put it together. Sorry, i gave you the impression i wanted you to do my work. If you can still help I would appreciate it.

Link to comment
Share on other sites

I fail to see the problem. I don't see how it matters if it's run against 5 computers or 5 million. It's the same amount of work to write the script regardless. And if anything, I'd rather it be ran against more, so I know I'm not wasting time for nothing (I wouldn't bother if it was for few computers) and it saves someone actual time. That is why we write scripts after all, saving time from doing time consuming tasks by hand.

It's only a matter of gluing 3 little snippets together anyway:

  1. get service tag from registry
  2. matching it with a line inside a simple text file
  3. setting a registry value

each snippet being already written for you for the most part. It's quick & trivial to do.

So here goes, and sorry for taking so long to get back to you. It's barely tested, and on Win7 x64 only. No error handling to speak of or anything fancy (could have matched it using a RegEx, could have added logging, you could fetch matches from a simple database instead or even a REST web service, etc). It should still work fine.

Sorry for having to attach it, but the [ code ] tag screws up lines (splits them in half), and the [ quote ] tags strip all indentation... You will have to rename it to .vbs obviously.

Edit, it looks like the line break thing is fixed, now it's only the colors that are a bit weird:


Option Explicit
On Error Resume Next

Const tsvfile = "\\Your-Server\Network-Share\Path\servicetag_resourcecode.txt" 'service tag -> resource file
const HKLM = &H80000002
Dim oWMI, oFSO, oReg, f, strTsv, colItems, objItem, strSvcTag, strRes, blnMatch

'get service tag
strSvcTag = ""
Set oWMI = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = oWMI.ExecQuery("SELECT * FROM Win32_BIOS")
For Each objItem In colItems
If (Not IsEmpty(objItem.SerialNumber) And Trim(objItem.SerialNumber) <> "") Then strSvcTag = objItem.SerialNumber
Next
If strSvcTag = "" Then WScript.Quit 'no service tag to work from

'find match in TSV file
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not(oFSO.FileExists(tsvfile)) Then WScript.quit 'no file to work from
Set f = oFSO.OpenTextFile(tsvfile, 1) 'ForReading
strTsv = f.ReadAll 'read the whole tsv file in one fell swoop to minimize I/O
f.Close
ColItems = Split(strTsv, vbCrLf)
blnMatch = false
strRes = ""
For Each objItem In ColItems
If left(objItem,len(strSvcTag)) = strSvcTag Then blnMatch = true
If blnMatch Then
strRes = right(objItem, len(objItem) - instr(objItem, chr(9)))
Exit For
End If
Next
If Not blnMatch Then WScript.Quit 'no match

'set description in registry
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
oReg.SetStringValue HKLM,"SYSTEM\CurrentControlSet\services\LanmanServer\Parameters","srvcomment",strRes

script.txt

Link to comment
Share on other sites

@CoffeeFiend. THANK YOU VERY MUCH :thumbup

I am going to start using this tomorrow after i get the txt file with the service tags and resource codes together. I understood to save it as a plain txt file not a csv file. Is that correct?

Have a great evening.

Link to comment
Share on other sites

You're welcome... Mind you it's barely worth being thanked for, I wouldn't call this a "production-grade" script. It's not tested enough to begin with. I hope the WMI part doesn't mess up on you (that's always the tricky part) -- it accounts for empty strings (IsEmpty) as well as zero-length strings and single "space" characters which it tends to return (WMI can always be tricky like this), but then again one could have checked it's length instead (i.e. that it's > 1 which would arguably be better -- then again parts of it suck by design e.g. including the TSV file's header row in the array). It also assumes the account running the script has sufficient permissions to set that registry value (no errors are displayed when it fails, no logs created or anything).

If the user accounts had sufficient permissions, then you could also set the computer description in AD (trivial to do, an extra 2 lines or so), but I wouldn't personally give the users these permissions. You could have another script that enumerates PCs from AD (trivial to do), runs the exact same WMI query to get the asset tag but against remote computers instead of locally, then matches it to a RES-xxxx tag (100% identical code), and sets it in AD (just run the script with an account that has sufficient permissions for this task). Although I would definitely prefer cscript here so you can do console output (show asset numbers & res tags; and also log them of course). Still quick and easy, but it would be a little trickier to test over here not having the same AD tree

I understood to save it as a plain txt file not a csv file. Is that correct?

I'm going with the sample you provided us with, specifically: a TSV file (tab separated values a.k.a. tab delimited). It might not be obvious if you open the file with notepad but it's a tab character between both columns and not a space, hence the chr(9) in the script. If it was a CSV file then it would be a comma instead. So you export your list from excel as a TSV file, and change the extension to .txt, just like you have before. If you want to use a comma or a space, there's a couple lines where you'd have to change minor things.

If you'd rather use another file format (CSV, flat text file with spaces, any database, a web service, etc) or language just let me know

Link to comment
Share on other sites

  • 2 years later...

hi all

can you update the script to change computer descraption from txt list of ip's or computer name's and custom descrpation foe each one

the list will be in server or any where you like or same folder , list type 'tab_separated_values' or any other

list will contain ip's and each ip will have new computer descraption

all this computer connect to domain and some win xp and some win 7

please can any one help me with it . i am nOOb

it will help alot

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...