Solved

VB script writes system info.

Posted on 2004-09-21
8
888 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
 

Author Comment

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

Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening Remote & Local Data Connection 2 51
Validating VB6 Function 19 56
VBS file using code from 2nd file (txt or vbs) 4 31
VB6 - Compare and highlight cell not the same 3 42
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

910 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

19 Experts available now in Live!

Get 1:1 Help Now