Scripts : Login Script to Collect Hardware Asset inventory of PC Desktops/Laptops

Here is the simple Login script to collect the asset inventory of the PC Desktop/Laptops.

This script  saves the asset details as two  individual text files for each PC, in the network share.

One file is for detailed inventory

report and another file contains only essential details in comma separated values(CSV) format, which can be easily imported to excel.

You can use this in script as login script in the active directory or call this script from your main login script. this will collect all the inventory details when user logs in the PC. So, you can easily trace a asset, where it is and who is using what informations

you can use txtcollector tool to merge all the small text files as a single file. So that you can import easily into MS Excel.  Post your comments or Post your doubts/questions in comments. I can revert back with the explanation.

This script was tested and used by for almost two years in a production environment..

Note:

Replace <\\YOUR NETWORK SHARED PATH\> according to your environment.
Create a Folder named Reports inside the network path what you are specifying in the script...


Copy the Script below                                                                                    


On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim SFileName
Dim sData
Dim sData1
Dim sFileName1
Dim s
Dim s1
Dim Mac

Dim sCompName 

arrComputers = Array(".")

For Each strComputer In arrComputers
 
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
  
   Set colItems1 = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
 

For Each objItem In colItems
    
sData= "Serial Number: " & objItem.IdentifyingNumber & vbcrlf     
sData= sData & "Model Type: " & objItem.Name & vbcrlf     
sData= sData & "UUID: " & objItem.UUID & vbcrlf      
sData= sData & "Vendor: " & objItem.Vendor & vbcrlf     
sData= sData & "Machine Model: " & objItem.Version & vbcrlf

sData1= objItem.IdentifyingNumber & ";" & objItem.Name & ";"  & objItem.Vendor & ";" & objItem.Version & ";"
      
Next

   For Each objItem In colItems1
   
  sData= sData & "UserName: " & objItem.UserName & vbcrlf
sData1= sData1 & objItem.UserName & ";"
   Next

Set battery = objWMIService.ExecQuery("SELECT * FROM Win32_Battery", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

  For Each objItem In battery
    
sData= sData & "BatteryID: " & objItem.DeviceID & vbcrlf   
sData= sData & "Name: " & objItem.Name & vbcrlf     
sData= sData & "SystemName: " & objItem.SystemName & vbcrlf

'sData1= sData1 & objItem.SystemName & ";"
sData1= sData1 & objItem.DeviceID & ";"

   Next

'sFileName= objItem.SystemName & "_Report.txt"
'sFileName1= objItem.SystemName & "_Report.txt"

sCompName = GetProbedID(StrComputer)

sData = sData & "Computer Name: " & sCompName
sData1 = sData1 & sCompName & ";"

sFileName = "<\\YOUR NETWORK SHARED PATH\>" & sCompName & ".txt"
sFileName1 = "<\\YOUR NETWORK SHARED PATH\>Report\" & sCompName & ".txt"

'Wscript.Echo sFileName & vbcrlf & sFileName1

'Wscript.Echo sCompName
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems2 = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems2
    WScript.Echo "MACAddress: " & objItem.MACAddress
Next



s = WriteFile(sData, sFileName)
s1 = WriteFile1(sData1, sFileName1)


Next

Function GetProbedID(sComp)
  Dim objWMIService, colItems, objItem
  Set objWMIService = GetObject("winmgmts:\\" & sComp & "\root\cimv2")
  Set colItems = objWMIService.ExecQuery("Select SystemName from " & _
                                         "Win32_NetworkAdapter",,48)
  For Each objItem in colItems
    GetProbedID =  objItem.PermanentAddress 'objItem.SystemName & "_"
Next
End Function


Function WriteFile(sData, sFileName)
  Dim fso, OutFile, bWrite
  bWrite = True
  Set fso = CreateObject("Scripting.FileSystemObject")
  On Error Resume Next
  Set OutFile = fso.OpenTextFile(sFileName, 2, True)
  'Possibly need a prompt to close the file and one recursion attempt.
  If Err = 70 Then
    Wscript.Echo "Could not write to file " & sFileName & ", results " & _
                 "not saved." & vbcrlf & vbcrlf & "This is probably " & _
                 "because the file is already open."
    bWrite = False
  ElseIf Err Then
    WScript.Echo err & vbcrlf & err.description
    bWrite = False
  End If
  On Error GoTo 0
  If bWrite Then
    OutFile.WriteLine(sData)
    OutFile.Close
  End If
  Set fso = Nothing
  Set OutFile = Nothing
  WriteFile = bWrite
End Function


Function WriteFile1(sData1, sFileName1)
  Dim fso, OutFile, bWrite
  bWrite = True
  Set fso = CreateObject("Scripting.FileSystemObject")
  On Error Resume Next
  Set OutFile = fso.OpenTextFile(sFileName1, 2, True)
  'Possibly need a prompt to close the file and one recursion attempt.
  If Err = 70 Then
    Wscript.Echo "Could not write to file " & sFileName & ", results " & _
                 "not saved." & vbcrlf & vbcrlf & "This is probably " & _
                 "because the file is already open."
    bWrite = False
  ElseIf Err Then
    WScript.Echo err & vbcrlf & err.description
    bWrite = False
  End If
  On Error GoTo 0
  If bWrite Then
    OutFile.WriteLine(sData1)
    OutFile.Close
  End If
  Set fso = Nothing
  Set OutFile = Nothing
  WriteFile = bWrite
End Function


End of script                                                                                                  

Share this

Related Posts

Previous
Next Post »

2 comments

Write comments
Anonymous
January 13, 2018 at 10:56 AM delete

If some one needs expert view on the topic of running a
blog then i advise him/her to pay a quick visit this webpage,
Keeep up the pleasant job.

Reply
avatar

What do you think about this Article? Add your Opinion..! EmoticonEmoticon