Link to home
Start Free TrialLog in
Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark

asked on

Loggin User- and Computerinformation to database

Does any of you guys out there have an idea on how to collect the following information and store it in a database (*.mdb prefered)?

The info I need to store is:
ComputerName
UserName
OSType, BuildVersion, ServicePack,
SerialNumber
Hardware, Make, Model
Memory, Processor, ProcessorSpeed

It is supposed to be used in a domain. My idea is to use it in a logon-script, but other suggestions are welcome.

Links and scripts are more than welcome.

Regards
Kasper K
Avatar of Nathan P
Nathan P
Flag of United States of America image

You can do some of what you are after using a PIPE from the command line to a text file.

How to move that to a database, I'm not so sure about.. but this should get you started:

>  This part of the script creates a file with the title of the PC name and notes who last logged onto that pc
echo Running WhoIsOn Script...
SET pc=%computername%
SET un=%username%
Echo %un% is using %pc% (LoginTime: %date% - %time%) > \\networkdrive\PC-User\%pc%.txt
echo Complete!

>  This part of the script creates a file with the title of the username and notes which PC was last logged onto by that user
echo Running WhoIsOn2 Script...
SET pc=%computername%
SET un=%username%
Echo %un% is using %pc% (LoginTime: %date% - %time%) > \\networkdrive\User-PC\%un%.txt
echo Complete!

>  This part of the script creates a file with the title of the PC name and notes a bunch of system information about that pc.

echo Updating I-Comm Network Asset DB...Please Wait.
@ping 127.0.0.1 -n 5 > NUL
systeminfo > \\networkdrive\Asset-%ComputerName%.txt
echo Complete!
Hi,

I would be happy if someone has or creates such script...
I wanted to share with what i have...

Save this script in "Filename.vbs"
Create a txt file "Computers.txt"
Put in all computer names inside it.Export the computernames from the ADS OU.

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Computers.txt")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\PCInfo.txt")
Dim objWMI
Do While Not objFile.AtEndOfStream
  intHDD1 = 0
  intHDD2 = 0
  strPC = objFile.ReadLine
  On Error Resume Next
  Set objWMI = GetObject("winmgmts:\\" & strPC & "\root\CIMV2")
  If Err.Number = 0 Then
    Set colItems = objWMI.ExecQuery("Select * From Win32_Processor")
    For Each objItem In colItems
      strProcessor = Replace(Replace(objItem.Name, "Intel", ""), "(R)", "") & "/"
    Next
    Set colItems = objWMI.ExecQuery("Select * From Win32_OperatingSystem")
    For Each objItem In colItems
      strRAM = FormatNumber(objItem.TotalVisibleMemorySize / 1024 / 1024, 2) & "GB Ram/"
    Next
   
    Set colItems = objWMI.ExecQuery("Select * From Win32_LogicalDisk")
      For Each objItem In colItems
        If Not IsNull(objItem.Size) Then
            isize = objItem.Size / 1024 / 1024 / 1024
            If UCase(objItem.DeviceID) = "C:" Or UCase(objItem.DeviceID) = "D:" Then
                intHDD1 = FormatNumber(intHDD1 + isize, 2)
            ElseIf UCase(objItem.DeviceID) = "E:" Or UCase(objItem.DeviceID) = "F:" Then
                intHDD2 = FormatNumber(intHDD2 + isize, 2)
            End If
        End If
      Next
    intHDD1 = intHDD1 & "GB/"
    intHDD2 = intHDD2 & "GB"

    Set colItems = objWMI.ExecQuery("Select * from Win32_FloppyDrive")
    If colItems.Count = 0 Then
      strFloppy = ""
    Else
      strFloppy = "/FDD/"
    End If
    Set colItems = objWMI.ExecQuery("Select * from Win32_CDROMDrive")
    If colItems.Count = 0 Then
      strCD = ""
    Else
      strCD = "/CDD/"
    End If
    Set colItems = objWMI.ExecQuery("Select * From Win32_NetworkAdapter")
    If colItems.Count > 0 Then
      strNIC = "/LAN"
    End If
    objOutput.WriteLine Replace(strPC & vbTab & strProcessor & strRAM & intHDD1 & intHDD2 & strFloppy & strCD & strNIC, "//", "/")
  Else
    objOutput.WriteLine strPC & " could not be contacted."
    On Error GoTo 0
  End If
  Set objWMI = Nothing
Loop

objFile.Close
objOutput.Close
Set objFile = Nothing
Set objOutput = Nothing
Set objWMI = Nothing
Set objFSO = Nothing

This gets the machines configuration..

Hope this helps...
See this like solution by Farhan
https://www.experts-exchange.com/questions/22757221/Find-which-user-has-presently-logged-in-a-machine.html
Find all users logged in all computers in a file
You should be able to use WMI objects to get system info? Check it out.

CT.
Use this:

using System.Management.Instrumentation;


ManagementObjectSearcher searcher =
  new ManagementObjectSearcher("Select Name from Win32_CDROMDrive");


for more options, refer:
http://msdn2.microsoft.com/en-us/library/Aa394554.aspx

Am sorry thot - it was in C# section..but u shud be able to use WMI in VB too..AFAIK.

CT.
Avatar of Kasper Katzmann

ASKER

I can't seem to get it to work :-( And just to confuse me further I've found the following code. It could vere well be exactly what I'm looking for, but... It is set up to work on a SQL database, but I don't have a SQL server. Is there a work-around? Maybe to make it work on an Access database?
'file 1
'**************************************************************
'*           Asset Checking                                   *
'*                                                            *
'*        Created By:                           *
'*        Create Date: 5/14/2004                              *
'*                                                            *
'*                                                            *
'**************************************************************
 
 
'**************************************************************
'*           Variable and Constant Declaration                *
'**************************************************************
 
Dim strVersion, oFS, SPVersion, IESPVersion
Dim BuildVersion, strSoftwareName, strSoftwarePath
Dim WshNetwork, WshShell, oFolder
Dim sLogFilePath
Dim sComputerName, sUserName, sRunLogs
Dim sRunLogPath, sRunLogFile, oFile, sLogonServer
Dim SOFTWARE_TEXT_FILE
Dim objFSR, objFSW, objReadF, objWriteF, strRecord
Dim objConnection, objRS
 
Const adOpenStatic = 3
Const adLockReadOnly = 1
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const ODBC_CONN_STRING = "Provider=SQLOLEDB;Data Source=prctsqla;Initial Catalog=WKSInfo;Integrated Security=SSPI;"
Const REG_KEY =  "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
Const REG_KEY_VALUE = "CSDVersion"
Const IEREG_KEY = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\"
Const IEREG_KEY_VALUE = "MINORVERSION"
 
 
'**************************************************************
'*             Main Processing Section                        *
'**************************************************************
 
On Error Resume Next
 
'**************************************************************
'*               Initialize Script Variables                  *
'**************************************************************
InitializeVariables()
 
'**************************************************************
'*          Call routine to see if software path exists       *
'**************************************************************
UpdateDatabase
 
'**************************************************************
'*               Initialize Script Variables                  *
'**************************************************************
Sub InitializeVariables()
 
      On Error Resume Next
      
        Set oFS = CreateObject("Scripting.FileSystemObject")
      Set WshNetwork = WScript.CreateObject("Wscript.Network")
      Set WSHShell   = WScript.CreateObject("WScript.Shell")
 
      sComputerName = WshNetwork.ComputerName
If UCASE(Mid(sComputerName,1,4)) = "GULF" Then
 
      sUserName     = WshNetwork.Username
      sLogonServer  = WshShell.ExpandEnvironmentStrings("%LOGONSERVER%")
      
      sRunLogPath = "C:\Temp"
      
      If oFS.FolderExists(sRunLogPath) = False Then
          Set oFolder = oFS.CreateFolder(sRunLogPath)
        End If
 
      sLogFilePath = sRunLogPath & "\LogoffChecking.log"
      Set oFile = oFS.OpenTextFile(sLogFilePath, ForReading, True)
 
 
      '**************************************************************
      '*            Read the BuildVersion                           *
      '**************************************************************
 
      SPVersion = WSHShell.RegRead(REG_KEY & REG_KEY_VALUE)
      IESPVersion = WSHShell.RegRead(IEREG_KEY & IEREG_KEY_VALUE)
Else
      wscript.quit
End If
End Sub
 
 
      '**************************************************************
      '*            Read the Computers hardware                     *
      '**************************************************************
Function UpdateDatabase
 
   On Error Resume Next
 
Dim strSerialNumber, strUsername, strFullName,strDomain
Dim strBuildVersion, strMake, strModel, strMemory, strProcessor
Dim strOStype, strProcessorSP, strDisk, sMsg1, sMsg2, strInstallDate
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colBIOS = objWMIService.ExecQuery _
("Select * from Win32_BIOS")
For Each objBIOS in colBIOS
strSerialNumber = objBIOS.SerialNumber
Next
 
strHardWare = WshShell.ExpandEnvironmentStrings("%WKSTYPE%")
If strHardWare = "%WKSTYPE%" Then
      strHardWare = ""
End If
 
Set objNetwork = WScript.CreateObject("WScript.Network")
strUsername = objNetwork.UserName
 
Set oSysInfo = CreateObject("ADSystemInfo")
sUserAdsPath = oSysInfo.UserName
Set oUser = GetObject("LDAP://" & sUserAdsPath)
strFullName = oUser.cn
strDomain = oSysInfo.DomainShortName
 
Set colSettings = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings 
strComputerName = objComputer.Name
strMake = objComputer.Manufacturer
strModel = objComputer.Model
strMemory = objComputer.TotalPhysicalMemory / 1024000
Next
 
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_Processor")
For Each objProcessor in colSettings 
strProcessor = LTrim(objProcessor.Name)
strProcessorSP = LTrim(objProcessor.currentclockSpeed)
Next
 
Set colSettings = objWMIService.ExecQuery _
    ("Select * from Win32_DiskDrive")
For Each objDisk in colSettings 
strDisk = LTrim(objDisk.Size) / 1024000
Next
 
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
strOStype = objOperatingSystem.Caption
 
strBuildVersion = objOperatingSystem.Organization
Dim dtmInstallDate, dtmConvertedDate
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
 
dtmConvertedDate.Value = objOperatingSystem.InstallDate
    dtmInstallDate = dtmConvertedDate.GetVarDate
Next
 
Set colInstalledPatches =  objWMIService.ExecQuery _
    ("Select * from Win32_QuickFixEngineering")
For Each objQuickFix in colInstalledPatches
   If sMsg2 = "File 1" Then
      sMsg2 =""
   End If
   If sMsg2 ="" Then
      sMsg2 = objQuickFix.HotFixID
   Else
    sMsg2 = sMsg2 & "; " & objQuickFix.HotFixID
   End If
Next
 
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
      If sMsg1 = "" Then
            sMsg1 = objPrinter.Name
      Else
          sMsg1 = sMsg1 & "; " & objPrinter.Name
      End If
Next
 
    UpdateDatabase = False
    
      Set objConnection = CreateObject("ADODB.Connection")
      set objRS         = CreateObject("ADODB.Recordset")
      objConnection.Open ODBC_CONN_STRING
 
 
      'strSQL = "Select * from WKSInfo where ComputerName = " & "'" & UCase(sComputerName) & "' OR SerialNumber = " & "'" & strSerialNumber & "'"
      strSQL = "Select * from WKSInfo where SerialNumber = " & "'" & strSerialNumber & "'"
 
      objRS.open strSQL, objConnection, adOpenStatic, adLockReadOnly
 
      If objRS.RecordCount = 0 Then
        'Insert a new row
         strSQL = "Insert INTO WKSInfo (ComputerName, UserName, FullName, OSType, BuildVersion, ServicePack, SerialNumber, Hardware, Make, Model, Memory, Processor, ProcessorSpeed, Printers, HotFixs, IEPatches, InstallDate, DateTime) " & _
                  "VALUES ( " & "'" & UCase(sComputerName) & "','" & _
                              UCase(sUserName) & "','" & _
                              strFullName & "','" & _
                              strOStype & "','" & _
                              strBuildVersion & "','" & _
                              SPVersion & "','" & _
                              strSerialNumber & "','" & _
                              strHardware  & "','" & _
                              strMake & "','" & _
                              strModel & "','" & _
                              strMemory & "','" & _
                              strProcessor & "','" & _
                              strProcessorSP & "','" & _
                              sMsg1 & "','" & _
                              sMsg2 & "','" & _
                              IESPVersion & "','" & _
                              dtmInstallDate & "','" & _
                              Now & "'" & ")"
        'msgbox strsql
         objConnection.Execute strSQL
         If Err.Number = 0 Then
            UpdateDatabase = True
         Else 
            sMsg = Err.Number & " " & Err.Description
            Err.Clear
            Call WriteLog(sLogFilePath, sComputerName, sUserName, BuildVersion, sMsg)
         End If
      Else
         'Do an update..
         strSQL = "UPDATE WKSInfo SET " & "ComputerName = '"     & UCase(sComputerName)     & "'," & _
                                     "UserName = '"        & UCase(sUserName) & "'," & _
                                     "FullName = '"        & strFullName & "'," & _
                                     "OSType = '"          & strOStype & "'," & _
                                     "BuildVersion = '"    & strBuildVersion     & "'," & _
                                     "ServicePack = '"    & SPVersion      & "'," & _
                                     "SerialNumber = '"    & strSerialNumber      & "'," & _
                                     "HardWare = '"        & strHardware  & "'," & _
                                     "Make = '"    & strMake      & "'," & _
                                     "Model = '"    & strModel      & "'," & _
                                     "Memory = '"    & strMemory      & "'," & _
                                     "Processor = '"    & strProcessor      & "'," & _
                                     "ProcessorSpeed = '" & strProcessorSP & "'," & _
                                     "Printers = '" & sMsg1 & "'," & _
                                     "HotFixs = '" & sMsg2 & "'," & _
                                     "IEPatches = '" & IESPVersion & "'," & _
                                      "InstallDate = '" & dtmInstallDate & "'," & _
                                     "DateTime = '"        & Now              & "' " & _
                                 "WHERE SerialNumber = '"    & strSerialNumber & "' "
                                    
        objConnection.Execute strSQL
        If Err.Number = 0 Then
           UpdateDatabase = True
        Else
            sMsg = Err.Number & " " & Err.Description
            Err.Clear
            Call WriteLog(sLogFilePath, sComputerName, sUserName, BuildVersion, sMsg)
        End If
      End If
'msgbox updatedatabase
End Function
'**************************************************************
'*  Log File Routine                                          *
'**************************************************************
Sub WriteLog(filepath, computer, user, bldversion, swname, sMsg)
      Dim oFSO, oFile
      Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set oFile = oFSO.OpenTextFile(filepath, ForAppending)
      oFile.WriteLine(Now & "," & computer & "," & user & "," & bldversion & "," & swname & "," & sMsg)
      oFile.Close
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of chandru_sol
chandru_sol
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Author, it clearly says either using Shell functions or WMI will help you. You may ort the SQL code to Access or use the links given. You may NOT get the code as you want from the net - You will have to understand and modify it.

CT.
Chandru looks to be great...
When i try i gt this...
C:\>PCinfo.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\PCinfo.vbs(50, 3) Microsoft JET Database Engine: Invalid SQL statement; expec
ted 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
Is there a way to run the script in silent mode?
The script should run in silent mode. Make sure you default script is wscript.exe

Sharath,

You need to create the msaccess file before you run the script

regards
Chandru
> The script should run in silent mode. Make sure you default script is wscript.exe

How does I verify this? And if it is not wscript.exe, how does I change it?

regards
Kasper
Chandru...
I created the file but still get the same...
cscript /h:wscript

regards
Chanrdu
Sharath

How did you create the file?

regards
Chandru
Opened an access file and saved it
You should create the table also....
I get this now chandru...

C:\>PCinfo.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\PCinfo.vbs(202, 6) Microsoft VBScript compilation error: Syntax error
Chandru can you explain a bit about the table please...