Quote:
> How can I know the free memory (free RAM) at a moment with
> a JScript.
One way to do it:
//////////////////////////////////////////////////////////////////////
// File: Mem.js
var oShell = new ActiveXObject("WScript.Shell");
if (!isCScript()||(WScript.Arguments.Length!=1)) {
showUsage();
WScript.Quit();
Quote:
}
var sHost = WScript.Arguments(0); // sHost can be Host Name or IP address
showMemInfo(sHost);
//////////////////////////////////////////////////////////////////////
function showMemInfo(sHost) {
try {
var oOSset = GetObject("winmgmts:{impersonationLevel=impersonate}//" + sHost).InstancesOf ("Win32_OperatingSystem")
} catch(e) {
switch (e.Number) {
case 462 :
// The remote server machine does not exist or is unavailable
WScript.Echo ("Host is not available.");
return;
case 429 :
// ActiveX component can't create object
WScript.Echo ("Host is missing WMI capabilities.");
return;
case -2147217405 :
// Lacking admim permissions to connect to host
WScript.Echo("Not enough permissions to connect to this host.");
return;
default :
// Whatever else the cause may be...
WScript.Echo(e.description);
return;
}
}
var oEnm_1 = new Enumerator(oOSset);
var oEnm_2;
WScript.Echo();
WScript.Echo("Host: " + sHost);
for (; !oEnm_1.atEnd(); oEnm_1.moveNext()) {
oEnm_2 = new Enumerator(oEnm_1.item().Properties_);
for (; !oEnm_2.atEnd(); oEnm_2.moveNext()) {
// Pick only memory related properties (ignore the rest):
switch (oEnm_2.item().Name) {
case "TotalVisibleMemorySize" :
WScript.Echo("\tTotal RAM -> " + Math.round(oEnm_2.item().Value/1024) + "\tMB");
continue;
case "FreePhysicalMemory" :
WScript.Echo("\tFree RAM -> " + Math.round(oEnm_2.item().Value/1024) + "\tMB");
continue;
case "FreeSpaceInPagingFiles" :
WScript.Echo("\tFree in page file -> " + Math.round(oEnm_2.item().Value/1024) + "\tMB");
continue;
case "TotalVirtualMemorySize" :
WScript.Echo("\tTotal virtual mem -> " + Math.round(oEnm_2.item().Value/1024) + "\tMB");
continue;
}
}
}
Quote:
}
function isCScript() { return (/cscript.exe$/i).test(WScript.FullName); }
function showUsage() {
var sMsg = "Syntax: \tC:\\>CScript //nologo " + WScript.ScriptName + " IP\r\n";
sMsg += " \t(must be run by 'CScript.exe')";
WScript.Echo(sMsg);
Quote:
}
// Branimir