Jump to content

Looking For Vbscript Code To Obtain Memory


Recommended Posts

Anyone know how to obtain how much memory of the PC which the script is running on? Ive been looking around and found a bunch of snippets which allow you to obtain this info for you computers on the network, but havent been able to figure out how to tweak it to only come back for the pc its being run on...

Thanks as always!

Link to comment
Share on other sites


Try this one...

' GetRAM.vbs,  Version 1.10
' Display amount of memory for the specified computer.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Initialize error message variable
strMsg = ""

' Check command line parameters
Select Case WScript.Arguments.Count
Case 0
 ' Default if none specified is local computer (".")
 Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
 Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
 For Each objItem in colItems
 strComputer = objItem.Name
 Next
Case 1
 ' Command line parameter can either be a computer
 ' name or "/?" to request online help
 strComputer = UCase( Wscript.Arguments(0) )
 if InStr( strComputer, "?" ) > 0 Then Syntax
Case Else
 ' Maximum is 1 command line parameter
 Syntax
End Select

' Enable error handling
On Error Resume Next

' Connect to specified computer
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
' Display error number and description if applicable
If Err Then ShowError

Set wbemServices = GetObject( "winmgmts://" & strComputer )
' Display error number and description if applicable
If Err Then ShowError

Set wbemObjectSet = wbemServices.InstancesOf( "Win32_LogicalMemoryConfiguration" )
' Display error number and description if applicable
If Err Then ShowError

For Each wbemObject In wbemObjectSet
strMsg = vbCrLf & "Total Physical Memory: " _
       & Int( ( wbemObject.TotalPhysicalMemory + 1023 ) / 1024 ) _
       & " MB" & vbCrLf
WScript.Echo strMsg
Next

'Done
WScript.Quit(0)


Sub ShowError()
strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf & _
         Err.Description & vbCrLf & vbCrLf
Syntax
End Sub


Sub Syntax
strMsg = strMsg & vbCrLf & "GetRAM.vbs,  Version 1.10" & vbCrLf _
       & "Display the amount of physical memory for any " _
       & "computer on the network" & vbCrLf & vbCrLf _
       & "Usage:  CSCRIPT  GetRAM.vbs  [ computer_name ]" _
       & vbCrLf & vbCrLf _
       & "Where:  " & Chr(34) & "computer_name" & Chr(34) _
       & " is the optional name of a remote" & vbCrLf _
       & "        computer (default is local computer name)" _
       & vbCrLf & vbCrLf _
       & "Written by Rob van der Woude" & vbCrLf _
       & "http://www.robvanderwoude.com" _
       & vbCrLf & vbCrLf _
       & "Based on the article " & Chr(34) _
       & "WMI Scripting Primer: Part 1" & Chr(34) & vbCrLf _
       & "by Greg Stemp, Dean Tsaltas and Bob Wells" & vbCrLf _
       & "http://msdn.microsoft.com/library/default.asp" _
       & "?url=/library/en-us/dnclinic/html/scripting06112002.asp" _
       & vbCrLf & vbCrLf
WScript.Echo strMsg
WScript.Quit(1)
End Sub

Link to comment
Share on other sites

Or maybe this ...

StrComputer = "."

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

   Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
   For Each objComputer In colSettings

wscript.echo objComputer.TotalPhysicalMemory / 1024 / 1024 &  " Mb"
  next

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...