Solved

VB script writes system info.

Posted on 2004-09-21
8
886 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
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Thanks.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:thunder_moose
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA Check for Character 9 69
Problem to the message 13 62
Vb.net 2008 2 49
vb6 connector to SQL Server 2 32
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now