• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 708
  • Last Modified:

WMI and Registry Query of Machine

We currently run a shutdown script that writes the computers service tag to the AD computer account description field. I am trying to expand this to also include the monitor service tag.

The monitor service tag can be found at HKLM\System\ControlSet001\Control\GraphicsDrivers\Connectivity\

The sub-key under connectivity is named after the serial number.

How can I have that serial number automatically record to an AD field for the respective computer? If possible, could the script below be modified so that on shutdown, the description is populated with the computer and monitor service tag.

Something like:

Computer: 1234567 Monitor:asldjaksdlk3jkasd
sn = GetSerialNumber 
UpdateDescription(sn) 
 
 
Function GetSerialNumber 
        strComputer = "." 
        Set objWMIService = GetObject("winmgmts:" _ 
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
        Set colBIOS = objWMIService.ExecQuery _ 
            ("Select * from Win32_BIOS") 
        For each objBIOS in colBIOS 
                GetSerialNumber = objBIOS.SerialNumber 
        Next 
End Function 
 
Sub UpdateDescription(strDescription) 
        Set objSysInfo = CreateObject("ADSystemInfo") 
         
        Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName) 
        objComputer.Description = strDescription 
        objComputer.SetInfo 
End Sub

Open in new window

0
Joseph Moody
Asked:
Joseph Moody
  • 4
  • 4
  • 2
1 Solution
 
Dhiraj MuthaLevel DCommented:
Hope the below code helps you.

Just the change the "value name" there (Line 21).
sn = GetSerialNumber 
Const HKEY_LOCAL_MACHINE = &H80000002
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
UpdateDescription(sn) 
 
 
Function GetSerialNumber 
        strComputer = "." 
        Set objWMIService = GetObject("winmgmts:" _ 
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
        Set colBIOS = objWMIService.ExecQuery _ 
            ("Select * from Win32_BIOS") 
        For each objBIOS in colBIOS 
                GetSerialNumber = objBIOS.SerialNumber 
        Next 
End Function 
 
Sub UpdateDescription(strDescription) 
	strKeyPath = "HKLM\System\ControlSet001\Control\GraphicsDrivers\Connectivity\"
	strValueName = "Value Name"
	oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
	Set objSysInfo = CreateObject("ADSystemInfo") 
         Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName) 
         objComputer.Description = "Computer: " & strDescription & " Monitor: " & strValue
         objComputer.SetInfo 
End Sub

Open in new window

0
 
RobSampsonCommented:
Hi, run this and see if it gives you the monitor number on its own.  If it does, we'll plug it into the script.

Regards,

Rob.
Const HKEY_LOCAL_MACHINE = &H80000002
strKey = "SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Connectivity"
strComputer = "."
Set objRegistry = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\default:StdRegProv")
If objRegistry.EnumKey(HKEY_LOCAL_MACHINE, strKey, arrKeyNames) = 0 Then
	If IsNull(arrKeyNames) = False Then
		For Each strKeyName In arrKeyNames
			MsgBox strKeyName
		Next
	End If
End If

Open in new window

0
 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
Hey Rob,

That worked! It popped up a message for each monitor connected.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
pspglb: What should be at line 21? The key below Connectivity is different for every machine. I know this is probably a silly question.
0
 
Dhiraj MuthaLevel DCommented:
Then I think Rob is the King.... Seriosuly he is a genius... I have just started my carrer in VBS on EE, thanks to Rob for that.
0
 
RobSampsonCommented:
@pspglb, thanks ;-)

JMoody, you say it showed a message for *each* monitor.....what do you want in the description of the computer object?  The first, second, or both?

Regards,

Rob.
0
 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
both if that is possible. Only a few computers have two monitors though
0
 
RobSampsonCommented:
Sure, it's possible.  Give this a shot.

Regards,

Rob.
sn = GetSerialNumber
arrMonitors = GetMonitorSerials
strDescription = "Computer: " & sn
For intMon = LBound(arrMonitors) To UBound(arrMonitors)
	strDescription = strDescription & " Monitor " & intMon + 1 & ": " & arrMonitors(intMon)
Next
'MsgBox strDescription
UpdateDescription(strDescription)

 
Function GetSerialNumber 
        strComputer = "." 
        Set objWMIService = GetObject("winmgmts:" _ 
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
        Set colBIOS = objWMIService.ExecQuery _ 
            ("Select * from Win32_BIOS") 
        For each objBIOS In colBIOS 
                GetSerialNumber = objBIOS.SerialNumber 
        Next 
End Function 
 
Sub UpdateDescription(strDescription) 
        Set objSysInfo = CreateObject("ADSystemInfo")          
        Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName) 
        objComputer.Description = strDescription 
        objComputer.SetInfo 
End Sub

Function GetMonitorSerials
	Const HKEY_LOCAL_MACHINE = &H80000002
	strKey = "SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Connectivity"
	strComputer = "."
	strMS = ""
	Set objRegistry = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\default:StdRegProv")
	If objRegistry.EnumKey(HKEY_LOCAL_MACHINE, strKey, arrKeyNames) = 0 Then
		If IsNull(arrKeyNames) = False Then
			For Each strKeyName In arrKeyNames
				If strMS = "" Then
					strMS = strKeyName
				Else
					strMS = strMS & ";" & strKeyName
				End If
			Next
		End If
	End If
	GetMonitorSerials = Split(strMS, ";")
End Function

Open in new window

0
 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
Worked perfectly!
0
 
RobSampsonCommented:
Great! Thanks for the grade.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now