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 =======

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

Educational App for Android Kids:

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

Comments

Popular posts from this blog

Copy a single file using robocopy

Copy a single file using robocopy from a local folder to a shared folder on the network.
A simple rule of thumb before any disaster strike, don't interchange the source and the destination.

If source and destination is mistakenly reverse, files might get overwritten. To avoid any loss of data do a test with a dummy file to ensure things work perfectly.
Robocopy [source][destination]   [file to be copied]
robocopy c:\local_c_folder  \\PC_network\shared_folder   file_to_be_copied_xx.txt
The command will be completed successfully provided the network access right has no issues.

Robocopy works quite good on large files. A simple copy or xcopy command will also work but the speed might vary.

Robocopy is free it can be accessed from command line. No need to install the resource kit tool if the operating system is Windows 7 or newer version.

Copy files with selected file extension using PowerShell and Robocopy:

$extension = ('.pdf', '.jpg', '.txt')
gci d:\WorkFolde…

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 sample output below will be generated if the co…