VBScript Inventory - Collecting several informations from Windows machines

From Tech-Wiki
Revision as of 22:57, 14 July 2016 by Fabricio.Lima (Talk | contribs) (Created page with "Category:Microsoft <nowiki> On error Resume next 'Collecting IP Address Function IP(strComputer) Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=im...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



On error Resume next

'Collecting IP Address
Function IP(strComputer) 

	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set IPConfigSet = objWMIService.ExecQuery _
    	("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
	For Each IPConfig in IPConfigSet
    	If Not IsNull(IPConfig.IPAddress) Then 
        	For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            	If Left(IPConfig.IPAddress(i),3) = "10." Then
            		'WScript.Echo "IP: " & IPConfig.IPAddress(i)
            		strResult = IPConfig.IPAddress(i)
            	End If
        	Next
    	End If
	Next

	IP = strResult
End Function

'Collecting MAC Address
Function MacAddress(strComputer) 

	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set IPConfigSet = objWMIService.ExecQuery _
    	("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
	For Each IPConfig in IPConfigSet
    	If Not IsNull(IPConfig.IPAddress) Then 
        	For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            	If Left(IPConfig.IPAddress(i),3) = "10." Then
            		'WScript.Echo "MAC: " & IPConfig.MACAddress(i)
            		strResult = IPConfig.MACAddress(i)
            	End If
        	Next
    	End If
	Next

	MacAddress = strResult
End Function

'Collecting Hostname
Function Hostname(strComputer) 

   Const wbemFlagReturnImmediately = &h10
   Const wbemFlagForwardOnly = &h20

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
	
    For Each objItem In colItems
      'WScript.Echo "Hostname: " & objItem.Name 
       strResult = objItem.Name
    Next

    Hostname = strResult
End Function

'Collecting logged on User
Function LoggedUser(strComputer) 

   Const wbemFlagReturnImmediately = &h10
   Const wbemFlagForwardOnly = &h20

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
	
   For Each objItem In colItems
       'WScript.Echo "UserName: " & objItem.UserName 
        strResult = objItem.UserName
   Next
    
   LoggedUser = strResult
End Function

'Collecting Manufacturer
Function Manufacturer(strComputer)

    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_BaseBoard")

    For Each objItem In colItems
    	'WScript.Echo "Manufacturer: " & objItem.Manufacturer 
    	strResult = objItem.Manufacturer
    Next
	
    Manufacturer = strResult
End Function

'Collecting Model
Function Model(strComputer) 

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
	
    For Each objItem In colItems
    	'WScript.Echo "Model: " & objItem.Name 
    	strResult = objItem.Name
    Next

    Model = strResult
End Function

'Collecting Free Memory
Function MemFree(strComputer)

   Const wbemFlagReturnImmediately = &h10
   Const wbemFlagForwardOnly = &h20

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
                                          
   For Each objItem In colItems
    	strResult = objItem.FreePhysicalMemory
   Next

   MemFree = strResult
End Function

'Collecting Installed Memory
Function Memory(strComputer) 

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMemory", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

    strResult = 0
    For Each objItem In colItems
    	'WScript.Echo "Memory Capacity: " & objItem.Capacity
    	strResult = strResult + objItem.Capacity
    Next
    
    Memory = strResult/1024/1024
End Function

'Collecting Hard Drive size
Function HDD(strComputer) 

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
                                          
    For Each objItem In colItems
    	'WScript.Echo "HDD Size: " & objItem.Size
    	strResult = objItem.Size
    Next
                                              
    HDD = CInt(strResult/1024/1024/1024)
End Function

'Collecting Processor
Function Processor(strComputer)

      Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor") 
	
      For Each objItem in colItems
      'Wscript.Echo "Processor: " & objItem.Name
           strResult = objItem.Name
      Next
	
      Processor = strResult
End Function

'Collecting Clock Speed (MHz)
Function Clock(strComputer) 

    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor") 
	
    For Each objItem in colItems
      'Wscript.Echo "Clock Speed: " & objItem.CurrentClockSpeed
      strResult = objItem.CurrentClockSpeed
    Next
	
    Clock = strResult
End Function

'Collecting Operating System
Function OperSystem(strComputer)

    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
	
    For Each objItem in colItems
      'WScript.Echo "Operational System: " & objItem.Caption
      strResult = objItem.Caption
    Next
 
   OperSystem = strResult
End Function

'Collecting Service Pack Version
Function ServicePack(strComputer)

    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem in colOperatingSystems
       strResult = objOperatingSystem.ServicePackMajorVersion  _
       & "." & objOperatingSystem.ServicePackMinorVersion
    Next
 
    ServicePack = strResult
End Function

'Collecting custom Registry Key
Function RegKey(strComputer) 

    strResult = "0"
    'Set WshShell = WScript.CreateObject("WScript.Shell")
    'strResult = WshShell.RegWrite("HKCU\Software\ACME\FortuneTeller\", 1, "REG_BINARY")
    'strResult = WshShell.RegWrite("HKCU\Software\ACME\FortuneTeller\MindReader", "Goocher!", "REG_SZ")
    'strResult = WshShell.RegRead("HKLM\SOFTWARE\ComputerAssociates\eTrustAntivirus\CurrentVersion")
	
    const HKEY_LOCAL_MACHINE = &H80000002, HKEY_CURRENT_USER = &H80000001

   Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

    strKeyPath = "SOFTWARE\Manufacturer\Software"
    strValueName = "value"
    objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strResult

    RegKey = strResult
End Function

'Collecting environment variable
Function VarLOGONSERVER(strComputer) 

    strResult = "0"
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Environment")

    For Each objItem in colItems
          If objItem.Name = "LOGONSERVER" Then
          strResult = objItem.VariableValue
        End If
    Next
	
    VarLOGONSERVER = strResult
End Function

'Collecting amount of Physical Cores
Function PhysicalCores(strComputer) 

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL") 
	
   For Each objItem  in colItems
      strResult = objItem.NumberOfCores
   Next

   PhysicalCores = strResult
End Function

'Collecting logical Processors
Function Processors(strComputer) 

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL") 
 
    For Each objItem  in colItems
        strResult = objItem.NumberOfLogicalProcessors
    Next

    Processors = strResult
End Function

'************ Begin Code ************

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const AllwaysCreate = 1

'Initializing report file
strFileRpt = InputBox("Select report file", "Inventory", "C:\report.txt")

If (strFileRpt <> "") then
    Set objFileRpt = objFSO.OpenTextFile(strFileRpt, ForAppending, AllwaysCreate)
    'Setting Header
    objFileRpt.WriteLine("Hostname;Manufacturer;Model;Memory (MB);Free Memory (KB);HDD (GB);Processor;Clock (MHz);Operational System;ServicePack;IP;PhysicalCores;Processors")
End if

'Getting hostname to connect as CLI parameter
'Set objHosts=WScript.Arguments

'If objHosts.count > 0 then
'   strComputer=objHosts(0)
'Else
'   strComputer="."
'End If

'Getting hostnames from a text file
strHosts = InputBox("Select hosts file", "Inventory", "C:\hosts.txt")
Set objHosts = objFSO.OpenTextFile(strHosts, ForReading)

Do Until objHosts.AtEndOfStream
   strComputer = objHosts.Readline

   WScript.Echo
   WScript.Echo "=========================================="
   WScript.Echo "Hostname: " & strComputer
   WScript.Echo "=========================================="

   'Starting collecting inventory for current host
   objFileRpt.WriteLine(Hostname(strComputer) & ";" & Manufacturer(strComputer) & ";" & Model(strComputer) & ";" & Memory(strComputer) & ";" & MemFree(strComputer) & ";" & HDD(strComputer) & ";" & Processor(strComputer) & ";" & Clock(strComputer) & ";" & OperSystem(strComputer) & ";" & ServicePack(strComputer) & ";" & IP(strComputer) & ";" & PhysicalCores(strComputer) & ";" & Processors(strComputer) & ";")

Loop

objFileRpt.Close
objHosts.Close