Forum Discussion
HKosova
Alumni
15 years agoHi Chris,
You can use WMI scripting classes to get memory usage on a remote computer. Specifically:
* get total physical memory using the Win32_ComputerSystem.TotalPhysicalMemory property,
* get free physical memory using the Win32_OperatingSystem.FreePhysicalMemory property,
* calculate memory usage as (total_memory - free_memory) / total_memory.
Here's an example:
Note that for the script to work, you may need to change security settings on the local and remote computers and allow WMI connections through the firewall as described in the following articles:
Working With WMI Objects in Scripts (TestComplete help)
Connecting to WMI on a Remote Computer
Connecting to WMI Remotely Starting with Windows Vista
Connecting Through Windows Firewall
You can use WMI scripting classes to get memory usage on a remote computer. Specifically:
* get total physical memory using the Win32_ComputerSystem.TotalPhysicalMemory property,
* get free physical memory using the Win32_OperatingSystem.FreePhysicalMemory property,
* calculate memory usage as (total_memory - free_memory) / total_memory.
Here's an example:
Sub Main
Dim strComputer, strLogin, strPassword, iMemUsage
strComputer = "computername" ' Use "." for local computer
strLogin = "domain\user" ' An empty string ("") means the currently used account on the local computer
strPassword = "password" ' An empty string ("") means the password of the currently used account on the local computer
iMemUsage = GetMemUsage(strComputer, strLogin, strPassword)
Log.Message "Memory usage on computer " & strComputer & ": " & iMemUsage
End Sub
' Returns % used physical memory on the specified computer
Function GetMemUsage(Computer, Login, Password)
Const FLAGS = 48 ' wbemFlagForwardOnly + wbemFlagReturnImmediately
Dim oLocator, oWMI, colItems, oItem, nTotalMem, nFreeMem
Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oWMI = oLocator.ConnectServer(Computer, "root\cimv2", Login, Password)
oWMI.Security_.ImpersonationLevel = 3 ' wbemImpersonate
' Get total physical memory, in kbytes
Set colItems = oWMI.ExecQuery("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem", , FLAGS)
For Each oItem in colItems
nTotalMem = CDbl(oItem.TotalPhysicalMemory) / 1024
Next
' Get free physical memory, in kbytes
Set colItems = oWMI.ExecQuery("SELECT FreePhysicalMemory FROM Win32_OperatingSystem", , FLAGS)
For Each oItem in colItems
nFreeMem = CDbl(oItem.FreePhysicalMemory)
Next
' Memory Usage = (Total - Free) / Total
GetMemUsage = Round((nTotalMem - nFreeMem) / nTotalMem * 100)
End Function
Note that for the script to work, you may need to change security settings on the local and remote computers and allow WMI connections through the firewall as described in the following articles:
Working With WMI Objects in Scripts (TestComplete help)
Connecting to WMI on a Remote Computer
Connecting to WMI Remotely Starting with Windows Vista
Connecting Through Windows Firewall