Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getram.vbs

(view source code of getram.vbs as plain text)

  1. ' GetRAM.vbs,  Version 1.10
  2. ' Display amount of memory for the specified computer.
  3. '
  4. ' Written by Rob van der Woude
  5. ' http://www.robvanderwoude.com
  6.  
  7. ' Initialize error message variable
  8. strMsg = ""
  9.  
  10. ' Check command line parameters
  11. Select Case WScript.Arguments.Count
  12. 	Case 0
  13. 		' Default if none specified is local computer (".")
  14. 		Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  15. 		Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
  16. 		For Each objItem in colItems
  17. 			strComputer = objItem.Name
  18. 		Next
  19. 	Case 1
  20. 		' Command line parameter can either be a computer
  21. 		' name or "/?" to request online help
  22. 		strComputer = UCase( Wscript.Arguments(0) )
  23. 		if InStr( strComputer, "?" ) > 0 Then Syntax
  24. 	Case Else
  25. 		' Maximum is 1 command line parameter
  26. 		Syntax
  27. End Select
  28.  
  29. ' Enable error handling
  30. On Error Resume Next
  31.  
  32. ' Connect to specified computer
  33. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  34. ' Display error number and description if applicable
  35. If Err Then ShowError
  36.  
  37. Set wbemServices = GetObject( "winmgmts://" & strComputer )
  38. ' Display error number and description if applicable
  39. If Err Then ShowError
  40.  
  41. Set wbemObjectSet = wbemServices.InstancesOf( "Win32_LogicalMemoryConfiguration" )
  42. ' Display error number and description if applicable
  43. If Err Then ShowError
  44.  
  45. For Each wbemObject In wbemObjectSet
  46. 	strMsg = vbCrLf & "Total Physical Memory: " _
  47. 	       & Int( ( wbemObject.TotalPhysicalMemory + 1023 ) / 1024 ) _
  48. 	       & " MB" & vbCrLf
  49. 	WScript.Echo strMsg
  50. Next
  51.  
  52. 'Done
  53. WScript.Quit(0)
  54.  
  55.  
  56. Sub ShowError()
  57. 	strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf & _
  58. 	         Err.Description & vbCrLf & vbCrLf
  59. 	Syntax
  60. End Sub
  61.  
  62.  
  63. Sub Syntax
  64. 	strMsg = strMsg & vbCrLf & "GetRAM.vbs,  Version 1.10" & vbCrLf _
  65. 	       & "Display the amount of physical memory for any " _
  66. 	       & "computer on the network" & vbCrLf & vbCrLf _
  67. 	       & "Usage:  CSCRIPT  GetRAM.vbs  [ computer_name ]" _
  68. 	       & vbCrLf & vbCrLf _
  69. 	       & "Where:  " & Chr(34) & "computer_name" & Chr(34) _
  70. 	       & " is the optional name of a remote" & vbCrLf _
  71. 	       & "        computer (default is local computer name)" _
  72. 	       & vbCrLf & vbCrLf _
  73. 	       & "Written by Rob van der Woude" & vbCrLf _
  74. 	       & "http://www.robvanderwoude.com" _
  75. 	       & vbCrLf & vbCrLf _
  76. 	       & "Based on the article " & Chr(34) _
  77. 	       & "WMI Scripting Primer: Part 1" & Chr(34) & vbCrLf _
  78. 	       & "by Greg Stemp, Dean Tsaltas and Bob Wells" & vbCrLf _
  79. 	       & "http://msdn.microsoft.com/library/default.asp" _
  80. 	       & "?url=/library/en-us/dnclinic/html/scripting06112002.asp" _
  81. 	       & vbCrLf & vbCrLf
  82. 	WScript.Echo strMsg
  83. 	WScript.Quit(1)
  84. End Sub
  85.  

page last uploaded: 2017-08-21, 14:26