Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for memory.ps

(view source code of memory.ps as plain text)

  1. # Memory.ps1,  Version 2.01
  2. # Display total and free amounts of physical memory in Bytes, KB and MB
  3. #
  4. # Usage:  ./Memory.ps1  [ remote_computer ]
  5. #
  6. # Written by Rob van der Woude
  7. # http://www.robvanderwoude.com
  8.  
  9. param( [string]$computer = "." )
  10.  
  11. if ( $HOME[0] -eq "/" ) {
  12. 	# Linux
  13. 	# Ignores computer name
  14. 	# Uses /proc/meminfo
  15. 	# Tip by silver_moon on Linux.com:
  16. 	# https://www.linux.com/blog/5-commands-check-memory-usage-linux
  17. 	[string[]]$freeStr = ( ( Select-String -Path /proc/meminfo -Pattern "MemAvailable" ) -Split ":" )[3].Trim( ) -Split "\s+"
  18. 	[string[]]$physStr = ( ( Select-String -Path /proc/meminfo -Pattern "MemTotal" ) -Split ":" )[3].Trim( ) -Split "\s+"
  19. 	[int64]$free = [int64]$freeStr[0] * ( "1" + $freeStr[1] )
  20. 	[int64]$phys = [int64]$PhysStr[0] * ( "1" + $physStr[1] )
  21. } else {
  22. 	# Windows
  23. 	# Uses WMI
  24. 	# Code generated by WMIGen:
  25. 	# http://www.robvanderwoude.com/wmigen.php
  26. 	$free = ( Get-WMIObject Win32_OperatingSystem -ComputerName $computer ).FreePhysicalMemory * 1KB
  27. 	$phys = ( Get-WMIObject Win32_OperatingSystem -ComputerName $computer ).TotalVisibleMemorySize * 1KB
  28. }
  29.  
  30. if ( $computer -ne "." ) {
  31. 	Write-Host
  32. 	Write-Host "Computer: $computer"
  33. }
  34. Write-Host
  35. Write-Host "Physical memory".PadRight( 16, " " ) -NoNewline
  36. Write-Host "Bytes".PadLeft( 14, " " ) -NoNewline
  37. Write-Host "KB".PadLeft( 11, " " ) -NoNewline
  38. Write-Host "MB".PadLeft( 8, " " )
  39. Write-Host ( "=" * 49 )
  40. Write-Host "Available".PadRight( 16, " " ) -NoNewline
  41. Write-Host ([string]$free).PadLeft( 14, " " ) -NoNewline
  42. Write-Host ([string][int]($free / 1KB)).PadLeft( 11, " " ) -NoNewline
  43. Write-Host ([string][int]($free / 1MB)).PadLeft( 8, " " )
  44. Write-Host "Total".PadRight( 16, " " ) -NoNewline
  45. Write-Host ([string]$phys).PadLeft( 14, " " ) -NoNewline
  46. Write-Host ([string][int]($phys / 1KB)).PadLeft( 11, " " ) -NoNewline
  47. Write-Host ([string][int]($phys / 1MB)).PadLeft( 8, " " )
  48. Write-Host

page last uploaded: 2015-12-04, 16:51