Wednesday, October 25, 2006

Computer Inventory the scripting way!

Working for a small to medium sized company has its benefits, like knowing everyone and being able to work on a ton of interesting projects, always testing your knowledge in many different areas of computing. So when the last Administrator left, he had worked there so long that a complete inventory never was complete. So I decided to record all of the computer assests we had. I got around to maybe 2 or 3 computers before I decided this was a waste of my time doing it by hand and foot.
I decided to pull out the trusty scripting pad and hack away. To make things easy the first thing I decided to do was head over to Microsoft and download the WMI Code Creator. Once downloaded it was all a matter of deciding which items I wanted to include in the report. At first I just wanted to populate a text file and I would have been happy. After a few trial runs I decided a text file just wasn't going to cut. I decided to move to the Excel sheet. Anyway below is the code I came up with. Since I have yet to find an append statement I decided to use a super secret column for counting and just adding one for each time the script is ran. The first part populates the column headers, which I will probably remove so it will import into Access and then upgrade to SQL.
My original program queried the remote computers, but then I realized the firewall's where blocking the the script from accessing the resources. So I decided to change that and just remote in and just run the script locally. If anyone has a better idea drop me an email. or leave it in a comment.
---------------------------------------------------------------------------------------------

'*******************************************
'* part 1
'* Change the UNC path to whatever you like
'* You are free to use this script, so make any changes
'* you would like
'********************************************

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
' Column Headers
' Network information
objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "IP Address"
objExcel.Cells(1, 3).Value = "MAC Address"
'Bios Information
objExcel.Cells(1, 4).Value = "Bios Manufacturer"
objExcel.Cells(1, 5).Value = "Build Name"
objExcel.Cells(1, 6).Value = "Bios Serial #"
'Memory Information
'objExcel.Cells(1, 7).Value = "Memory Bank Label"
'objExcel.Cells(1, 8).Value = "Memory Capacity"
'Video Information
objExcel.Cells(1, 7).Value = "Video Description"
objExcel.Cells(1, 8).Value = "Video Driver Version"
objExcel.Cells(1, 9).Value = "Installed Display Driver"
'Processor Information
objExcel.Cells(1, 10).Value = "Processor Name"
'CDROM/DVD information
objExcel.Cells(1, 11).Value = "CDROM/DVD Name"
'Basic Computer Information
objExcel.Cells(1, 12).Value = "Computer Manufacturer"
objExcel.Cells(1, 13).Value = "Computer Model"
objExcel.Cells(1, 14).Value = "Computer Total Physical Memory"
objExcel.Cells(1, 15).Value = "Number of Processors"
'Network Adapter Information
objExcel.Cells(1, 16).Value = "Network Adapter Description"
'Disk Drive Information
objExcel.Cells(1, 17).Value = "Disk Drive Name"
objExcel.Cells(1, 18).Value = "Disk Drive Size"
objExcel.Cells(1, 19).Value = 1
'Configure the columns
objExcel.Range("A1:R1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
objExcel.ActiveWorkbook.SaveAs("\\Server\Folder\ComputerInventory.xls")
objExcel.ActiveWorkbook.Close
objExcel.Quit

-----------------------------------------------------------------------------------

'*********************************************
'Part 2
'Script to gather computer hardware information
'saves information into excel document.
'
'hardcoded the link to the excel document.
'
'*******************************

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Open("
\\Server\Folder\ComputerInventory.xls")
intRowAdapter = 2
intRowCDDVD = 2
intRowMemory = 2
intRowNet = 2
intRow = objExcel.Cells(1, 19).Value + 1
objExcel.Cells(1, 19).Value = intRow

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colAdapters = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
objExcel.Cells(intRow, 1).Value = objAdapter.DNSHostName

If Not IsNull(objAdapter.IPAddress) Then
objExcel.Cells(intRow, 2).Value = objAdapter.IPAddress
End If
objExcel.Cells(intRow, 3).Value = objAdapter.MACAddress
Exit For
Next

'Bios Information
Set colBIOS = objWMIService.ExecQuery ("Select * from Win32_BIOS")
For Each objBIOS in colBIOS
objExcel.Cells(intRow, 4).Value = objBIOS.Manufacturer
objExcel.Cells(intRow, 5).Value = objBIOS.Name
objExcel.Cells(intRow, 6).Value = objBIOS.SerialNumber
Next

'Video Information
Set colVideo = objWMIService.ExecQuery ("Select * from Win32_VideoController")
For Each objVideo in colVideo
objExcel.Cells(intRow, 7).Value = objVideo.Description
objExcel.Cells(intRow, 8).Value = objVideo.DriverVersion
objExcel.Cells(intRow, 9).Value = objVideo.InstalledDisplayDrivers
Next

'Processor Information
Set colProcessor = objWMIService.ExecQuery ("SELECT * from Win32_Processor")
For Each objProcessor in colProcessor
objExcel.Cells(intRow, 10).Value = objProcessor.Name
Next

'CDROM/DVD information
Set colCDROM = objWMIService.ExecQuery ("SELECT * from Win32_CDROMDrive")
For Each objCDROM in colCDROM
objExcel.Cells(intRow, 11).Value = objCDROM.Caption
Next
'Basic Computer Information
Set colCompInfo = objWMIService.ExecQuery ("SELECT * from Win32_ComputerSystem")
For Each objCompInfo in colCompInfo
objExcel.Cells(intRow, 12).Value = objCompInfo.Manufacturer
objExcel.Cells(intRow, 13).Value = objCompInfo.Model
objExcel.Cells(intRow, 14).Value = objCompInfo.TotalPhysicalMemory
objExcel.Cells(intRow, 15).Value = objCompInfo.NumberOfProcessors
Next

'Network Adapter Information
Set colNetworkAdapter = objWMIService.ExecQuery ("SELECT * from Win32_NetworkAdapter")
For Each objNetworkAdapter in colNetworkAdapter
objExcel.Cells(intRow, 16).Value = objNetworkAdapter.Description
Exit For
Next

'Disk Drive Information
Set colDiskDrive = objWMIService.ExecQuery ("SELECT * from Win32_DiskDrive")
For Each objDiskDrive in colDiskDrive
objExcel.Cells(intRow, 17).Value = objDiskDrive.Model
objExcel.Cells(intRow, 18).Value = objDiskDrive.Size
Next

objExcel.Cells.EntireColumn.AutoFit

objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Quit
Set objWMIService = Nothing
Set objExcel = Nothing
Set colAdapters = Nothing
Set colObjects = Nothing
Wscript.Echo "Done"

No comments: