Announcement

Collapse
No announcement yet.

collect network asset information via a VB script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • collect network asset information via a VB script

    anyone knows a script to collect pc model and serial numbers in a network, all computers are windows XP professional

  • #2
    Re: collect network asset information via a VB script

    How about using some WMI? Look at Win32_BaseBoard class. For a quick run, try this:

    Code:
    wmic baseboard get serialnumber
    -vP

    Comment


    • #3
      Re: collect network asset information via a VB script

      Try this vbscript I put together a little while ago. It will read the desired computer names from a text file, and then grab the model and serial number of the computer outputting it into an excel file. You can probably replace what I have using "Win32_BaseBoard", but this seemed to work best for me when I was working on this. I highlighted the main part of the script in blue:


      Code:
       ' text file to read from
       strReadFile = "C:\computers.txt"
      
       ' excel file to create
       sXLS = "C:\service tags.xls"  
      
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       Set objTS = objFSO.OpenTextFile(strReadFile)
       Set objShell = CreateObject("WScript.Shell")
      
       Set objExcel = CreateObject("Excel.Application")
      	objExcel.Application.DisplayAlerts = False
      	objExcel.Visible = True
      
       	objExcel.Workbooks.Add
      
      	' define the column titles
          	objExcel.Cells(1,1).Value = "Computer Name"
          	objExcel.Cells(1,2).Value = "Model"
         	objExcel.Cells(1,3).Value = "Service Tag"
      
            	xRow = 1
            	yColumn = 1
      
      	' apply styles to rows and columns
         	Do Until yColumn = 4
             		objExcel.Cells(xRow,yColumn).Font.Bold = True
          		objExcel.Cells(xRow,yColumn).Font.Size = 11
          		objExcel.Cells(xRow,yColumn).Interior.ColorIndex = 11 
          		objExcel.Cells(xRow,yColumn).Interior.Pattern = 1
          		objExcel.Cells(xRow,yColumn).Font.ColorIndex = 2
          		objExcel.Cells(xRow,yColumn).Borders.LineStyle = 1
          		objExcel.Cells(xRow,yColumn).WrapText = True
      	yColumn = yColumn + 1
            	Loop
      
      	x = 2
      	y = 1
      
        ' start reading from the text file, until the end
        Do Until objTS.AtEndOfStream
          strComputer = objTS.ReadLine
      
      		' check if the computername is pingbale, if not then skip to next name
      		If (IsPingable(strComputer) = True) then
        		   Set objWMIService = GetObject("winmgmts:" _
      			& "{impersonationLevel=impersonate}!\\" _
      			& strComputer & "\root\cimv2")
      
      			Set colComputer = objWMIService.ExecQuery _
      				("SELECT * FROM Win32_ComputerSystemProduct","WQL",48)
      			y1 = y
      
      			If Err.number=0 Then
        				For Each objComputer in colComputer
            					objExcel.Cells(x,y1).Value = strComputer
            					y1 = y1 + 1 ' go to next column
            					objExcel.Cells(x,y1).Value = objComputer.Name
            					y1 = y1 + 1 ' go to next column
            					objExcel.Cells(x,y1).Value = objComputer.IdentifyingNumber
            					x = x + 1 ' go to the next Row
      				Next
      
      			Else
            					objExcel.Cells(x,y1).Value = strComputer
            					y1 = y1 + 1 ' go to next column
            					objExcel.Cells(x,y1).Value = "Model not found!"
            					y1 = y1 + 1 ' go to next column
            					objExcel.Cells(x,y1).Value = "Serial not found!"
            					x = x + 1 ' go to the next Row
      			End If
      			Err.clear
      
      		Else
            			objExcel.Cells(x,y1).Value = strComputer
            			y1 = y1 + 1 ' go to next column
            			objExcel.Cells(x,y1).Value = "Not Pingable"
            			x = x + 1 ' go to the next Row
      			
        	    	End If
         Loop
      
       objExcel.Columns("A:C").Select
       objExcel.Selection.HorizontalAlignment = 3 	'center all data
       objExcel.Selection.Borders.LineStyle = 1 	'apply borders
       objExcel.Columns("A:AH").EntireColumn.AutoFit  'autofit all columns
      
       Const Excel07 = 12
       appVerInt = split(objExcel.Version, ".")(0)
      	If appVerInt- Excel07 >=0 Then
        	    objExcel.ActiveWorkbook.SaveAs(sXLS), 56  'You use office 2007 / 2010
      	Else
        	    objExcel.ActiveWorkbook.SaveAs(sXLS), 43  'You use office 97-2003
      	End If
      
       objExcel.Quit
      
       set objExcel = Nothing
       objTS.Close
      
      
      msgbox "Done!"
      WScript.Quit
      
      
      Function IsPingable(ByVal strHost)
        If Trim(strHost) <> "" Then
           strCommand = "Ping.exe -n 3 -w 750 " & strHost
           Set objExecObject = objShell.Exec _
              ("%comspec% /c title " & strHost _
              & chr(38) & strCommand)
           Do While Not objExecObject.StdOut.AtEndOfStream
              strText = objExecObject.StdOut.ReadLine()
              If Instr(strText, "TTL=") > 0 _
                Then IsPingable = True : Exit Do
           Loop
           If IsPingable = True then
              With GetObject("winmgmts:root\cimv2")
                 For Each objProcess in .ExecQuery _
                    ("SELECT commandline FROM Win32_Process" _
                    & " WHERE Name = 'ping.exe'",,48)
                    If objProcess.commandline = strCommand _
                      Then objProcess.Terminate() : Exit For
                 Next
              End With
           End If
        End If
        If (not IsPingable = True) Then IsPingable = False
      End Function
      Last edited by Rems; 1st February 2011, 21:29.

      Comment


      • #4
        Re: collect network asset information via a VB script

        Originally posted by vonPryz View Post
        How about using some WMI? Look at Win32_BaseBoard class. For a quick run, try this:

        Code:
        wmic baseboard get serialnumber
        -vP

        I think this might actually get the serial number of the motherboard, and not the whole system.

        Comment


        • #5
          Re: collect network asset information via a VB script

          Originally posted by ekrengel View Post
          I think this might actually get the serial number of the motherboard, and not the whole system.
          Yes it does return an other serial#.


          Using the Win32_ComputerSystem class you can get the computer 'model'. And by using the Win32_BIOS class you can read the serialnumber of the computer.

          However instead of the use of two classes you could also use just the Win32_ComputerSystemProduct class, see sample.

          simple vbs sample
          Code:
          strComputer = InputBox(vbNewLine&vbNewLine& _
              "Enter name of the Computer To Query", _
              "Model information from remote computer",".")
          
          ' - - -
          Set objWMIService = GetObject _
               ("winmgmts:{impersonationLevel=impersonate}\\"  _
               & strComputer & "\root\CIMV2")
          
          Set colItems = objWMIService.ExecQuery _
               ("SELECT * FROM Win32_ComputerSystemProduct","WQL",48)
          
          For Each objItem In colItems
             strVendor = objItem.Vendor
             strModel = objItem.Name
             strVersion = objItem.Version
             strSerial = objItem.IdentifyingNumber
          Next
          
          wsh.echo "Computer name:", strComputer _
                & vbNewLine & "Vendor:", strVendor _  
                & vbNewLine & "Model:", strModel _
                & vbNewLine & "Version:", strVersion _
                & vbNewLine & "Serial:", strSerial _
                & vbNewLine & vbNewLine
          
          Set objWMIService = Nothing
          ' - - -
          
          wscript.quit
          Instead of using the inputbox, you could extend the code so the script can read the names of the computers from a file OR, extend it to make it possible to retrieve all computeraccount names directly from a certain OU(s) from the Active Directory.

          When running the script with a collection of computernames it is recommended to use the ping test from ekrengel's script to determine if a computer is connectable before trying to connect to it.


          \Rems


          EDIT
          here's a simple batch with the inputbox, the batch will do the same as the above vbs sample .
          Code:
          @echo off & color 6a
          Title Model information from remote computer
          
                         echo\Enter the name of
          Set /p "strComputer=Computer To Query: "
          echo\&Call Set "strComputer=%strComputer%"
          
          echo\Computername: %strComputer%
          wmic /NODE:"%strComputer%" /IMPLEVEL:impersonate PATH Win32_ComputerSystemProduct GET Vendor, Name, Version, IdentifyingNumber
          
          echo\...done!&pause>nul
          
          ::----------------------------------------------------------------------
          :: http://msdn.microsoft.com/en-us/library/aa394531(VS.85).aspx
          ::
          :: /NODE
          :: => * Computer names, comma delimited. All commands are synchronously
          ::      executed against all computers listed in this value. 
          ::    * File names must be prefixed with &. Computer names within
          ::      a file must be on separate lines OR comma delimited.
          ::----------------------------------------------------------------------
          Last edited by Rems; 11th May 2009, 20:01.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: collect network asset information via a VB script

            Yes that actually works much better, and faster since it is not using two classes.

            I've updated my first post of the script to reflect the changes.

            Comment


            • #7
              Re: collect network asset information via a VB script

              Originally posted by ekrengel View Post
              Yes that actually works much better, and faster since it is not using two classes.

              I've updated my first post of the script to reflect the changes.

              I just registered to say THANKS! This was a really useful script and just what I was looking for

              Comment


              • #8
                Re: collect network asset information via a VB script

                Hello to all,
                I just want to thank all of you for the good information posted in this threat, which is exactly what I was trying to accomplish when I submitted a post yesterday. Thankfully Rems suggested to me this threat.
                I have a question: I ran the script that ekrengel posted here, it collect the information I need but sometimes it errors out with the following message:
                Error: The remote server machine does not exist or is unavailable: 'GetObject'
                Code: 800A01CE
                Source: Microsoft VBScript runtime error.
                Then no spreadsheed is created. I don't know if this is related to permissions or firewall?
                Thanks for your help,
                Alex
                Last edited by Alexito; 30th September 2009, 15:38.

                Comment


                • #9
                  Re: collect network asset information via a VB script

                  As a workaround you can add "On Error Resume Next" to move on to the next computer without breaking the script.

                  Does the script error out on the same computers each time?

                  Comment


                  • #10
                    Re: collect network asset information via a VB script

                    Aquadodo,
                    Thanks for your suggestion, once I inserted the line "on error resume next" before the color part on ekrengel's script, it worked like a charm...
                    Thanks again,
                    Alex

                    Comment

                    Working...
                    X