0

I've been working on a VBscript inspired by these posts: http://deployhappiness.com/series/creating-an-inventory-with-active-directory/ and I've found a decent amount of success in my test machines with the Manufacturer, Model Series, MTM, and Serial Number entries. That being said I've run into a brick wall on uploading CPU Name and installed physical memory to the custom attributes(AD atributes like "RAM" "processor" etc.) I have set up. All machines being used for testing these scripts have been given the appropriate SELF permissions to write to these attributes using the scripts and I'm testing them locally on each machine before I put them into a netlogon script.

The code I've been trying to use for both goes something like this, and I've consulted WMIExplorer for guidance without much luck on getting the two scripts to work:

strprocessor= GetCPU
updatecpu(strprocessor)
Function GetCPU
strcomputer = "."
set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & strcomputer & "root\cimv2")
                set colprocessors = objWMIservice.ExecQuery _
                ("Select * from Win32_Processor")
                For each objprocessor in colprocessors
                GetCPU= objprocessor.name
                Next
End Function
Sub Updatecpu(strprocessor)
Set objSysInfo = CreateObject("ADSystemInfo")
On Error Resume Next
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
objProcessor.name = strprocessor
objComputer.SetInfo
End Sub

And the memory function:

   strRAM= GetRAM
    updateRAM(strRAM)

    Function GetRAM
    strComputer = "*"
        Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate} !\\" & strcomputer & "root\cimv2")
        set colcomputersystem = objWMIService.ExecQuery _ 
        ("Select * from Win32_Computersystem")
        For each objcomputersystem in colcomputersystem
        GetRAM= objcomputersystem.totalphysicalmemory
        Next
    End Function        
Sub UpdateRAM(strRAM)
            Set objSysInfo = CreateObject ("ADSystemInfo")
            On Error Resume Next
            Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
            objcomputersystem.totalphysicalmemory = strRAM
            objComputer.SetInfo
        End Sub

By contrast the manufacturer component that worked was formatted almost identically:

Strmanufacturer = getmanufacturer
updatemanufacturer(strmanufacturer)
Function Getmanufacturer   
        strComputer = "."   
        Set objWMIService = GetObject("winmgmts:" _   
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")   
        Set colcomputersystem = objWMIService.ExecQuery _   
            ("Select * from Win32_computersystem")   
        For each objcomputersystem in colcomputersystem   
                Getmanufacturer = objcomputersystem.manufacturer 
        Next   
End Function   
Sub Updatemanufacturer(strmanufacturer)
Set objSysInfo = CreateObject("ADSystemInfo")
On Error Resume Next
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
objComputer.manufacturer = strmanufacturer
objComputer.SetInfo
End Sub

I know I had to screw up either an obj or col category here but I haven't been able to figure it out just yet. Any suggestions?

nerdythor
  • 1
  • 4

1 Answers1

0

First, use

Debug your script using On Error GoTo 0 as follows:

Sub Updatecpu(strprocessor)
On Error GoTo 0          ' don't continue in case of severe error
Set objSysInfo = CreateObject("ADSystemInfo")
''' On Error Resume Next
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
objProcessor.name = strprocessor
objComputer.SetInfo
End Sub

With On Error Resume Next you should establish at least basic error handling to make public incidental error code(s) and message(s):

Sub Updatecpu(strprocessor)
On Error GoTo 0          ' don't continue in case of severe error
Set objSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)

On Error Resume Next     ' continue, I use my own error handling approach
objProcessor.name = strprocessor
If Err.Number <> 0  Then
    msgbox "error 0x" & Hex( Err.Number) & " " & CStr( Err.Number) _
         & " >" & Err.Description & "< " & Err.Source
End If

Err.Clear                ' clear the Err object after an error has been handled
objComputer.SetInfo
If Err.Number <> 0  Then
    msgbox "error 0x" & Hex( Err.Number) & " " & CStr( Err.Number) _
       & " >" & Err.Description & "< " & Err.Source
End If

On Error Goto 0         ' return back to system error handling
End Sub

For instance, at the first glance: objcomputersystem variable has its local scope in Getmanufacturer function and is not defined in Updatemanufacturer procedure; analogously, objProcessor variable has its local scope in GetCPU function and is not defined in Updatecpu procedure... Read VBScript Variables MSDN article.

Another issue: your GetCPU function will return unique value in case of multiprocessor machine (the last item in colprocessors collection).

Note that I keep my answer in VBScript topic; I know nothing about ADSystemInfo object properties and LDAP protocol.

JosefZ
  • 1,564
  • 1
  • 10
  • 18
  • While I did check the article, I don't think it's the structure of the script's language since the manufacturer, model, and serial number all return the information in the way desired. I've triplechecked the permissions to read/write in each of the /Self Dependent Computer Objects OUs I want to write in and they're all good. Because the directory I'm checking is different my gut feeling is that it's something funky in the obj or col tags that I'm missing. – nerdythor Nov 25 '15 at 16:13