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
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
Post a Comment