Solved

VB script writes system info.

Posted on 2004-09-21
8
890 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

829 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