Tuesday, December 1, 2009

Hardware inventory during the user login and write to a excel (From:http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_22575735.html)

The script write to an existing Excel spreadsheet, on the first sheet, and output the same results, creating a new row if that Pc is new, and overwriting the existing row if it already exists.
Pleae change the strWorkbook string to the UNC path (or mapped drive if all of your clients use the same drive) to point to an Excel template file, that has one sheet in it.  The file must exist first.
'=-=============================
'On Error Resume Next

' To convert to a logon script that runs without user interaction, add a rem in front of the strComputer = InputBox
' line below and remove the rem from the strComputer = "." line below that. This will then only check the PC on which
' the script runs
Set WshNetwork = WScript.CreateObject("WScript.Network")

strComputer = "."
strWorkBook = "N:\Scripting\Test Scripts\Hardware Inventory Script\Hardware.xls"

' Create Excel Spreadsheet
Set app = CreateObject("Excel.Application")
app.Workbooks.Open strWorkBook
Set wb = app.ActiveWorkbook
app.Visible = False
wb.Activate
Set ws = wb.Worksheets(1)
ws.Cells(1,1).Value = "Computername"
ws.Columns(1).ColumnWidth = 20
ws.Cells(1,2).Value = "Username"
ws.Columns(2).ColumnWidth = 30
ws.Cells(1,3).Value = "Manufacturer"
ws.Columns(3).ColumnWidth = 20
ws.Cells(1,4).Value = "Model"
ws.Columns(4).ColumnWidth = 20
ws.Cells(1,5).Value = "Serial Number"
ws.Columns(5).ColumnWidth = 30
ws.Cells(1,6).Value = "CPU"
ws.Columns(6).ColumnWidth = 30
ws.Cells(1,7).Value = "CPU Speed"
ws.Columns(7).ColumnWidth = 10
ws.Cells(1,8).Value = "Operating System"
ws.Columns(8).ColumnWidth = 40
ws.Cells(1,9).Value = "Service Pack"
ws.Columns(9).ColumnWidth = 20
ws.Cells(1,10).Value = "Total Memory"
ws.Columns(10).ColumnWidth = 20
ws.Cells(1,11).Value = "Audit Date"
ws.Columns(11).ColumnWidth = 20
ws.Rows(1).Font.Bold = True

' Get the serial number first to see if it already exists in the spreadsheet
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem In colItems
strSerialNumber = "" & objItem.SerialNumber
Next
Set objWMIService = Nothing
Set colItems = Nothing

intRowToUse = -1
For intRowCount = 2 To ws.UsedRange.Rows.Count
      If Trim(strSerialNumber) = Trim(ws.Cells(intRowCount,5).Value) Then
            intRowToUse = intRowCount
      End If
Next
If intRowToUse = -1 Then
      intRowToUse = ws.UsedRange.Rows.Count + 1
End If

' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,1).Value = "" & objItem.Caption
    ws.Cells(intRowToUse,2).Value = "" & objItem.UserName
    ws.Cells(intRowToUse,3).Value = "" & objItem.Manufacturer
    ws.Cells(intRowToUse,4).Value = "" & objItem.Model
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
'Output the Serial Number
ws.Cells(intRowToUse,5).Value = strSerialNumber

'
' Get CPU Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,6).Value = "" & objItem.Name
    ws.Cells(intRowToUse,7).Value = "" & objItem.CurrentClockSpeed
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
    ws.Cells(intRowToUse,8).Value = "" & objItem.Caption
    ws.Cells(intRowToUse,9).Value = "" & objItem.CSDVersion
    ws.Cells(intRowToUse,10).Value = "" & FormatNumber(objItem.TotalVisibleMemorySize/1024,0)  
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get & Writeout current Date
ws.Cells(intRowToUse,11).value = "" & Day(Now) & "-" & Month(Now) & "-" & Year(Now)

'Autofit all columns
app.ActiveSheet.Columns.EntireColumn.AutoFit
' Save Audit File
app.DisplayAlerts = False
'wb.Saved = True
wb.Close True
app.DisplayAlerts = False
app.quit

MsgBox "Done"
'=====================

 


http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_22575735.html

Print Friendly and PDF
Share/Bookmark

No comments:

Post a Comment