Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add WMI Query to Existing VBscript

Posted on 2010-08-27
2
Medium Priority
?
1,145 Views
Last Modified: 2013-11-08
We have an existing logon Vbscript that writes information to each computer's and user's description field in Active Directory and so far it works great.  We have found the need to add a wmi query of each computer's serial number to the script's output as well.

Here is the current code that we are using:

''POPULATES AD WITH USER LOGON INFO IN DESCRIPTION FIELD
 
Set objSysInfo = CreateObject("ADSystemInfo")

unEscapedUserName = Replace(objSysInfo.UserName, "/", "\/")
Set objUser = GetObject("LDAP://" & unEscapedUserName)

Set objUser = GetObject("LDAP://" & Replace(objSysInfo.UserName, "/", "\/"))
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)

 
strMessage1 = " Authenticated on: " & objComputer.CN & " at " & Now & ".  MAC: " & MACAddresses(".")
strMessage2 = " MAC: " & MACAddresses(".") & "." & " Authenticated by: " & objUser.CN & " at " & Now & "."

objUser.Description = strMessage1
objUser.SetInfo

objComputer.Description = strMessage2
objComputer.SetInfo

function MACAddresses(strComputer)
      dim objWMI, colItems, objItem
      dim strMac
      strMac=""  
      Set objWMI = GetObject("winmgmts:\\" & strComputer &  "\root\CIMV2")  
      Set colItems = objWMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE",,48)
      For Each objItem in colItems
            if objItem.MACAddress<>"" then strMac=strMac & objItem.MACAddress & vbCrLf
      Next  
      MACAddresses = strMac
end function

I found the following snippet that queries and returns what we need to add; however it obviously just returns screen output.  

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BaseBoard")
For Each objItem in colItems
    For Each strOption in objItem.ConfigOptions
    Next
    Wscript.Echo "Serial Number: " & objItem.SerialNumber
Next

How can we add the above wmi query to write the Serial Number to both of the following output lines (in our working logon script)?
strMessage1 = " Authenticated on: " & objComputer.CN & " at " & Now & ".  MAC: " & MACAddresses(".")
strMessage2 = " MAC: " & MACAddresses(".") & "." & " Authenticated by: " & objUser.CN & " at " & Now & "."

I'd like the Serial Number query to be at the end of the strMessage1 line and at the beginning of the strMessage2 line.

Is this possible?
0
Comment
Question by:CommCatz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
kemi67 earned 2000 total points
ID: 33541431
I am sorry but it is seems to me that you have already written the solution.
Do you want to know how to modify your original script?
Try this:

''POPULATES AD WITH USER LOGON INFO IN DESCRIPTION FIELD
 
Set objSysInfo = CreateObject("ADSystemInfo")

unEscapedUserName = Replace(objSysInfo.UserName, "/", "\/")
Set objUser = GetObject("LDAP://" & unEscapedUserName)

Set objUser = GetObject("LDAP://" & Replace(objSysInfo.UserName, "/", "\/"))
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)

strSerial= SerialNumber
strMessage1 = " Authenticated on: " & objComputer.CN & " at " & Now & ".  MAC: " & MACAddresses(".") & strSerial
strMessage2 = strSerial & " MAC: " & MACAddresses(".") & "." & " Authenticated by: " & objUser.CN & " at " & Now & "."

objUser.Description = strMessage1
objUser.SetInfo
objComputer.Description = strMessage2
objComputer.SetInfo

function MACAddresses(strComputer)
      dim objWMI, colItems, objItem
      dim strMac
      strMac=""  
      Set objWMI = GetObject("winmgmts:\\" & strComputer &  "\root\CIMV2")  
      Set colItems = objWMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE",,48)
      For Each objItem in colItems
            if objItem.MACAddress<>"" then strMac=strMac & objItem.MACAddress & vbCrLf
      Next  
      MACAddresses = strMac
end function

function SerialNumber
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BaseBoard")
For Each objItem in colItems
    For Each strOption in objItem.ConfigOptions
    Next
    SerialNumber = "Serial Number: " & objItem.SerialNumber
Next
end function


I hope I have understood your question

kemi67
0
 

Author Closing Comment

by:CommCatz
ID: 33541518
Exactly what I was looking for!  Thanks!
0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question