Solved

VB script writes system info.

Posted on 2004-09-21
8
893 Views
Last Modified: 2008-02-20
Hello,

I need a sample VB script file which will write system info.(OS,version,computer name, current user,Physical Memory, IP Addr, MAC Addr,Harddrive space..) into a text file. The text file needs to be located on desktop.

Thanks.
0
Comment
Question by:mrong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12111602
Your best bet lies with the WMI.  You can get some details on this at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_start_page.asp
0
 
LVL 3

Expert Comment

by:thunder_moose
ID: 12112853
Hi there,

As the previous poster mentioned, you can use WMI to do it. Here's a VBScript for WSH that does what you want. To use it, open notepad, paste the code there, save it as a .vbs file and use cscript from the command promt to run it as follows: cscript filename.vbs

It saves the info in a file called info.txt on the desktop. This script assumes you have Windows 2000 or later.

Dim strComputerName, strDesktopPath
Dim objWMIService, objQryOS, objOS
Dim objQryNetwork, objNIC, objQryDisks, objDisk
Dim objQryCompSys, objComp
Dim objWSHShell
Dim objFSO, objTxtInfo

strComputername = "."

Set objWSHShell = CreateObject("WScript.Shell")
strDesktopPath = objWSHShell.SpecialFolders("Desktop")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxtInfo = objFSO.CreateTextFile(strDesktopPath & "\Info.txt")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & strComputerName & "\root\cimv2")

Set objQryOS = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
Set objQryNetwork = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", , 48)
Set objQryDisks = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk")
Set objQryCompSys = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComp in objQryCompSys
    objTxtInfo.WriteLine "User Name: " & objComp.UserName
    objTxtInfo.WriteLine "Computer Name: " & objComp.Name
    objTxtInfo.WriteLine "Physical Memory: " & objComp.TotalPhysicalMemory & " bytes"
Next

For Each objOS In objQryOS
    objTxtInfo.Writeline "OS Name: " & objOS.Name
    objTxtInfo.WriteLine "OS Version: " & objOS.Version
Next

For Each objNIC in objQryNetwork
    If Not IsNull(objNic.IPAddress) Then
       For I=LBound(objNIC.IPAddress) to UBound(objNIC.IPAddress)
           objTxtInfo.WriteLine "IP Address: " & objNIC.IPAddress(I)
       Next
       objTxtInfo.WriteLine "MAC Address: " & objNIC.MACAddress
    End If
Next

For Each objDisk in objQryDisks
    objTxtInfo.WriteLine "Disk Drive: " & objDisk.DeviceID & _
                         "(" & objDisk.FreeSpace & " bytes free)"
Next

objTxtInfo.Close

Set objTxtInfo = Nothing
Set objFSO = Nothing
Set objWSHShell = Nothing
Set objWMIService = Nothing


Hope that helps you,
Cheers,
thunder_moose
0
 

Author Comment

by:mrong
ID: 12114273
Hi thunder_moose,

I got OS Name: Microsoft Windows 2000 Professional|C:\WINNT|\Device\Harddisk0\Partition1. Is it possible to get rid of " |C:\WINNT|\Device\Harddisk0\Partition1" ?

Also your VBscript will get free hard drive space, Can you get available space as well? I only need disk space info for drive C and D. How to change the code?

Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mrong
ID: 12114305
Is this code also work for WinXP? what happens if run it on Win98 or 95?

Thanks.
0
 
LVL 3

Expert Comment

by:thunder_moose
ID: 12115230
Hi Mrong,

To answer your last questions first, yes the script will work on Windows XP. It can work on Windows 98, ME and NT4.0 as well but you'll have to install WSH for Windows 9x. You can get that here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=0a8a18f6-249c-4a72-bfcf-fc6af26dc390&displaylang=en

I'm not sure if it'll work on 95, but can't think of a real reason why it shouldn't.

As for your other queries. The first is simple text manipulation. Replace the line

   objTxtInfo.Writeline "OS Name: " & objOS.Name

with

   strOSName = objOS.Name
   intEnd = instr(strOSName, "|") - 2
   strOSName = Mid(strOSName, 1, intEnd)
   objTxtInfo.Writeline "OS Name: " & strOSName

To show the capacity of the drive, just add this line:

    objTxtInfo.WriteLine "Total Disk Space: " & objDisk.Size

In the section dealing with the hard disk size (under For Each objDisk...)

If you want to convert it to MegaBytes instead, use: objDisk.Size / (1024*1024)

Once again, I hope this helps
thunder_moose
0
 
LVL 3

Accepted Solution

by:
thunder_moose earned 80 total points
ID: 12115357
Oh yes, almost missed your other question:

To only display drives C & D's sizes you have to change the diskdrive code to:

If objDisk.DeviceID = "c:" OR objDisk.DeviceID = "d:" Then
.....
End if

But I realise that all this might be pretty confusing, so here's the entirety of the code again, with the modifications that you requested.

Dim strComputerName, strDesktopPath
Dim objWMIService, objQryOS, objOS
Dim objQryNetwork, objNIC, objQryDisks, objDisk
Dim objQryCompSys, objComp
Dim objWSHShell
Dim objFSO, objTxtInfo

strComputername = "."

Set objWSHShell = CreateObject("WScript.Shell")
strDesktopPath = objWSHShell.SpecialFolders("Desktop")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxtInfo = objFSO.CreateTextFile(strDesktopPath & "\Info.txt")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & strComputerName & "\root\cimv2")

Set objQryOS = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
Set objQryNetwork = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", , 48)
Set objQryDisks = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk")
Set objQryCompSys = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComp in objQryCompSys
    objTxtInfo.WriteLine "User Name: " & objComp.UserName
    objTxtInfo.WriteLine "Computer Name: " & objComp.Name
    objTxtInfo.WriteLine "Physical Memory: " & objComp.TotalPhysicalMemory & " bytes"
Next

For Each objOS In objQryOS
   strOSName = objOS.Name
   intEnd = instr(strOSName, "|") - 2
   strOSName = Mid(strOSName, 1, intEnd)
   objTxtInfo.Writeline "OS Name: " & strOSName
   objTxtInfo.WriteLine "OS Version: " & objOS.Version
Next

For Each objNIC in objQryNetwork
    If Not IsNull(objNic.IPAddress) Then
       For I=LBound(objNIC.IPAddress) to UBound(objNIC.IPAddress)
           objTxtInfo.WriteLine "IP Address: " & objNIC.IPAddress(I)
       Next
       objTxtInfo.WriteLine "MAC Address: " & objNIC.MACAddress
    End If
Next

For Each objDisk in objQryDisks
     If (objDisk.DeviceID = "C:") OR (objDisk.DeviceID = "D:") Then
        objTxtInfo.WriteLine "Disk Drive: " & objDisk.DeviceID & _
                         "(" & objDisk.FreeSpace & "of " & objDisk.Size & " bytes free)"
     End If
Next

objTxtInfo.Close

Set objTxtInfo = Nothing
Set objFSO = Nothing
Set objWSHShell = Nothing
Set objWMIService = Nothing



There you go. That should take care of everything you wanted.

0
 

Author Comment

by:mrong
ID: 12115770
What about manufacturer and model? How to convert bytes to MB for disk drive and memory.
Thanks.


0
 
LVL 3

Expert Comment

by:thunder_moose
ID: 12120280
Well no offense, but for 80 points, I'm done with this question. Follow up on the link posted by the other poster for more info on WMI.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question