Skip to main content

VBScript Get Servers Data

VBScript Get HDD Space, Memory Usage, and CPU Usage

As the odds say, old habits are hard to break.

If you've been around working for IT for quite some time.

I'm sure you had fallen in love with VBScript.

VBScript has been around for quite some time and still does it magic and able to deliver good results.

VBScript is quite straight forward, all you need is a notepad and a keyboard.

Of course, it sounds simple, but it's really not.

What I mean is you don't need to install other software to get VBScript up and running.

Of course, you need to have Windows OS and a piece of code in mind to write something on the notepad.

The "I Love You" virus was a VBScript and a lot of computers fall in love with it.

Well, I'm not encouraging someone to write a virus.

Which other people really love to do it as their past time or a way of living.

The piece of VBScript below is able to get hard disk space on any server specified on the list.

It will get the hard disk space on your server; all the drives available on your system.

If you have Drive C, Drive D, Drive E ... etc.. the script will get the data for those drives.

Even if you have an external HDD drive, or a USB Thumb Drive connected on your system.

The script will also get the available space for those drives.

The script also gets the available memory and the processor usage, but of course, the data taken is the data that was read during the time the script is executed.

If you really want to monitor the processor usage and memory usage to get the server performance.

You need to use other tools available on Windows or third party tools, just search the web for this kind of tools.

To run VBScript code below, you need to have the Administrative rights or privileges for all the system or servers that you need to get the data.

Copy the code below to a notepad, and save it to any file name you want.

On saving the code make sure to enclose the file name in quotation marks such as "getdata.vbs"

If the quotation marks are not included, the file name will have .txt appended to the file name.

If you are login as with appropriate admin privileges, you can copy the code below to a notepad and save it as "filename.vbs".

Since you're login as admin, you can just double-click on the file and it will run.

But if you're not login with any admin privileges, the script will run but with incorrect result or data.

Script can also be run from a command prompt by typing: cscript filename.vbs

There are lines you need to change before running the code to suit your environment.

Change Web1, Web2 and FileSrv1 to the computer name you want to get the data.

Dim servers(2)
servers(0)="Web1"
servers(1)="Web2"
servers(2)="FileSrv1"

Change this line also to the specified location where you want to save the data and change filename output of your choice also.

'Path to save output data
sFileName="c:\recordtxt\SRVS101013.txt"

Output result will have this format:

Server Name ===> Web1 <===
DeviceID: C: 100.62258529663086-GB
DeviceID: D: 94.0740089416504-GB
Free Memory: 3047MB
CPU Usage: 1%

Server Name ===> Web2 <===
DeviceID: C: 3121.38264083862-GB
DeviceID: D: 5753.55364227295-GB
Free Memory: 7527MB
CPU Usage: 0%

Server Name ===> FileSrv1 <===
DeviceID: C: 5121.38264083862-GB
DeviceID: D: 5753.55344009399-GB
DeviceID: E: 122.5816040039063-GB
Free Memory: 8527MB
CPU Usage: 11%


======= CODE SNIPPET START =======

Dim oWsh, oWshSysEnv, objFSO, objWMIService,x ,strComputer,fso,OutFile,WriteFile,memData,cpuData,hddData
Dim oDrives, oDrive, objOutFile, colItems, objItem,colDisks,colMem,sFileName,xTotalDisk
Dim strLineDate, strLineTime, strLineProcessorTime, strLineDriveSpace, strLinePercentCommittedBytesInUse

Dim servers(2)
servers(0)="Web1"
servers(1)="Web2"
servers(2)="FileSrv1"

'=================================
Const HARD_DISK = 3
Const CONVERSION = 1073741824 ' This is total bytes in 1 GB.
Const CONVERT = 1048576 ' This is total bytes in 1 MB.

For Each x In servers

strComputer = x

'Display computer name to check if the loop is working (optional)
'msgbox strComputer

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Get HDD Space
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "") _

'Gets MEMORY Usage
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colMem= objWMIService.ExecQuery( _
    "SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory",,48)

'Gets PROCESSOR Usage
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'")

'===============================

Set oWsh = WScript.CreateObject("WScript.Shell")
Set oWshSysEnv = oWsh.Environment("PROCESS")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")

strLineDate = Date()
strLineTime = Time()

'=============================
'Path to save output data

sFileName="c:\recordtxt\SRVS101013.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
  On Error Resume Next
  Set OutFile = fso.OpenTextFile(sFileName, 8, True)
'==============================

sData="Server Name ===> " & x & " <==="

outfile.WriteLine(sData)

For Each objDisk in colDisks

xTotalDisk = objDisk.freespace / CONVERSION

hddData = "DeviceID: " & vbTab &  objDisk.DeviceID & vbTab & xTotalDisk & "-GB"

outfile.WriteLine(hddData)

Next

'============================
'Get Available Memory

For Each objItem in colMem
    'Wscript.Echo "-----------------------------------"
    'Wscript.Echo "Win32_PerfFormattedData_PerfOS_Memory instance"
    'Wscript.Echo "-----------------------------------"
    'Wscript.Echo "AvailableMBytes: " & objItem.AvailableMBytes
memData = "Free Memory: " & vbTab & objItem.AvailableMBytes & "MB"

Next
outfile.WriteLine(memData)
'===============================

cpuData=""

'============================
'Get Processor Usage
For Each objItem In colItems
 strLineProcessorTime =  objItem.PercentProcessorTime
 cpuData = "CPU Usage: " & vbTab & strLineProcessorTime & "%"
 'msgbox cpuData
 outfile.WriteLine(cpuData)
Next
'===============================

sData="=====xxxx-xxxx====="

sdata=""
memData=""
cpuData=""
hddData=""

Next

======= CODE SNIPPET END =======

VBScript still rocks!

==================================

Educational App for Android Kids:

https://play.google.com/store/apps/details?id=com.letsmultiply

Comments

Popular posts from this blog

Notepad++ convert multiple lines to a single line and vice versa

Notepad++ is an awesome text editing tool, it can accept regex to process the text data. If the data is in a “.csv” format or comma separated values which is basically just a text file that can either be opened using a text editor, excel or even word. Notepad++ can process the contents of the file using regex. Example if the data has multiple rows or lines, and what is needed is to convert the whole lines of data into a single line. Notepad++ can easily do it using regex. However, if the data is on a single line and it needs to be converted into multiple lines or rows then regex can also be used for this case. Here’s an example on how to convert multiple rows or lines into a single line. Example data: Multiple rows, just a sample data. Press Ctrl+H, and  on "Find what" type: [\r\n]+ and on "Replace with" type with: , (white space) --white space is needed if need to have a space in between the data. See image below, "Regular Expression" must be se

WMIC get computer name

WMIC get computer model, manufacturer, computer name and  username. WMIC is a command-line tool and that can generate information about computer model, its manufacturer, its username and other informations depending on the parameters provided. Why would you need a command line tool if there’s a GUI to check? If you have 20 or 100 computers, or even more. It’s quite a big task just checking the GUI to check the computer model and username. If you have remote computers, you need to delegate someone in the remote office or location to check. Or you can just write a batch file or script to automate the task. Here’s the code below on how get computer model, manufacturer and the username. Open an elevated command prompt and type:     wmic computersystem get "Model","Manufacturer", "Name", "UserName" Just copy and paste the code above, the word “computersystem” does not need to be change to a computer name. A

How to check office version from command line

The are quite a few ways to check office version it can be done via registry, PowerShell or VBScript and of course, good old command line can also do it. Checking Windows office version whether it is Office 2010, Office, 2013, Office 2016 or other version is quite important to check compatibility of documents; or just a part of software inventory. For PowerShell this simple snippet can check the office version: $ol = New-Object -ComObject Excel.Application $ol . Version The command line option will tell you where’s the path located; the result will also tell whether office is 32-bit, 64-bit and of course the version of the office as well. Here’s the command that will check the office version and which program directory the file is located which will tell whether it’s 32-bit or 64-bit. Command to search for Excel.exe: DIR C:\ /s excel.exe | find   /i "Directory of"  Above command assumes that program files is on  C: drive. Sample Outpu