Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for powershellcomputerclass.ps

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

  1. class Computer
  2. {
  3. 	static [string]$Version = '1.00'
  4.  
  5. 	static [string]$Copyrights = 'Rob van der Woude'
  6.  
  7. 	static [string]$ProductPage = 'https://www.robvanderwoude.com/powershellcomputerclass.php'
  8.  
  9. 	static [string]$Help = @"
  10.  
  11. Computer class, Version {0}
  12. Make basic hardware infomation available in a class
  13.  
  14. Copyrights {1} {2}
  15. {3}
  16.  
  17.  
  18. How to use:
  19. ===========
  20.  
  21. Create an instance of the Computer class using the following command:
  22.  
  23. `$computer = [Computer]::New( )
  24.  
  25. After gathering hardware information for a couple of seconds, `$computer will have the information available through its properties and methods:
  26.  
  27. ---------        ---------        -------------------------------    ----------
  28. Property:        DataType:        Information on this computer's:    WMI class:
  29. ---------        ---------        -------------------------------    ----------
  30. Name             System.String    computer name
  31. IPAddress        System.String    local IP address
  32. BIOS             System.Object    BIOS                               root/CIMV2/Win32_BIOS
  33. CDROM            System.Object    CD-ROM drive(s)                    root/CIMV2/Win32_CDROMDrive
  34. CPU              System.Object    processor(s)                       root/CIMV2/Win32_Processor
  35. HDD              System.Object    harddisk(s)                        root/Microsoft/Windows/Storage/MSFT_PhysicalDisk
  36. Keyboard         System.Object    keyboard(s)                        root/CIMV2/Win32_Keyboard
  37. MainBoard        System.Object    main board                         root/CIMV2/Win32_BaseBoard
  38. Memory           System.Object    memory                             root/CIMV2/Win32_PhysicalMemory
  39. Monitor          System.Object    monitor(s)                         root/WMI/WmiMonitorID
  40. Mouse            System.Object    mouse (or mice)                    root/CIMV2/Win32_PointingDevice
  41. NIC              System.Object    physical network adapter(s)        root/CIMV2/Win32_NetworkAdapter
  42. Printers         System.Object    physical local printer(s)          root/CIMV2/Win32_Printer
  43. Slots            System.Object    system slots                       root/CIMV2/Win32_SystemSlot
  44. Sound            System.Object    sound card(s)                      root/CIMV2/Win32_SoundDevice
  45. USB              System.Object    USB controller(s)                  root/CIMV2/Win32_USBController
  46. Video            System.Object    video card(s)                      root/CIMV2/Win32_VideoController
  47. Windows          System.Object    Windows version                    root/CIMV2/Win32_OperatingSystem
  48. WinSat           System.Object    WinSat system scores               root/CIMV2/Win32_WinSAT
  49.  
  50. ---------        ---------        ----------                         ----------
  51. Property:        DataType:        Number of:                         WMI class:
  52. ---------        ---------        ----------                         ----------
  53. _AGP             System.Int32     AGP slots                          root/CIMV2/Win32_SystemSlot
  54. _AGP2X           System.Int32     AGP2X slots                        root/CIMV2/Win32_SystemSlot
  55. _AGP4X           System.Int32     AGP4X slots                        root/CIMV2/Win32_SystemSlot
  56. _AGP8X           System.Int32     AGP8X slots                        root/CIMV2/Win32_SystemSlot
  57. _Cardbus         System.Int32     Cardbus slots                      root/CIMV2/Win32_SystemSlot
  58. _CDROMs          System.Int32     CD-ROM drives                      root/CIMV2/Win32_CDROMDrive
  59. _COMPorts        System.Int32     serial ports                       root/CIMV2/Win32_SerialPort
  60. _EISA            System.Int32     EISA slots                         root/CIMV2/Win32_SystemSlot
  61. _HDDs            System.Int32     harddisk drives                    root/Microsoft/Windows/Storage/MSFT_PhysicalDisk
  62. _ISA             System.Int32     ISA slots                          root/CIMV2/Win32_SystemSlot
  63. _LPTPorts        System.Int32     parallel ports                     root/CIMV2/Win32_ParallelPort
  64. _MemoryGB        System.Int32     GigaBytes of memory                root/CIMV2/Win32_PhysicalMemory
  65. _MemoryModules   System.Int32     memory modules                     root/CIMV2/Win32_PhysicalMemory
  66. _MemorySockets   System.Int32     memory sockets                     root/CIMV2/Win32_PhysicalMemoryArray
  67. _Monitors        System.Int32     monitors                           root/WMI/WmiMonitorID
  68. _PCI             System.Int32     PCI slots                          root/CIMV2/Win32_SystemSlot
  69. _PCIE            System.Int32     PCI-E slots                        root/CIMV2/Win32_SystemSlot
  70. _PCIX            System.Int32     PCI-X slots                        root/CIMV2/Win32_SystemSlot
  71. _PCMCIA          System.Int32     PCMCIA slots                       root/CIMV2/Win32_SystemSlot
  72. _USB2            System.Int32     USB controllers (except USB 3)     root/CIMV2/Win32_USBController
  73. _USB3            System.Int32     USB 3 controllers                  root/CIMV2/Win32_USBController
  74. _VESA            System.Int32     VESA slots                         root/CIMV2/Win32_SystemSlot
  75.  
  76.  
  77. Static Members:
  78. ===============
  79.  
  80. -----------------------           ------------                       ------
  81. Properties and Methods:           Description:                       Value:
  82. -----------------------           ------------                       ------
  83. [Computer]::Version               This class' version                {0}
  84. [Computer]::Copyrights            Author's name                      {2}
  85. [Computer]::ProductPage           This class' webpage                {3}
  86. [Computer]::Help                  This help text
  87. [Computer]::OpenProductPage( )    Open this class' webpage
  88. [Computer]::New( )                Create new instance of this class
  89.  
  90. "@ -f [Computer]::Version, [char]0x00A9, [Computer]::Copyrights, [Computer]::ProductPage
  91.  
  92. 	static [void]OpenProductPage( )
  93. 	{
  94. 		Start-Process -FilePath $( [Computer]::ProductPage )
  95. 	}
  96.  
  97. 	[string]$Name = $Env:ComputerName
  98.  
  99. 	[string]$IPAddress = $( [System.Net.Dns]::Resolve( $Env:ComputerName ).AddressList.IPAddressToString )
  100.  
  101. 	[object]$BIOS
  102.  
  103. 	[object]$CDROM
  104.  
  105. 	[object]$CPU
  106.  
  107. 	[object]$HDD
  108.  
  109. 	[object]$Keyboard
  110.  
  111. 	[object]$MainBoard
  112.  
  113. 	[object]$Memory
  114.  
  115. 	[object]$Monitor
  116.  
  117. 	[object]$Mouse
  118.  
  119. 	[object]$NIC
  120.  
  121. 	[object]$Printers
  122.  
  123. 	[object]$Slots
  124.  
  125. 	[object]$Sound
  126.  
  127. 	[object]$USB
  128.  
  129. 	[object]$Video
  130.  
  131. 	[object]$Windows
  132.  
  133. 	[object]$WinSat
  134.  
  135. 	[int]$_AGP
  136.  
  137. 	[int]$_AGP2X
  138.  
  139. 	[int]$_AGP4X
  140.  
  141. 	[int]$_AGP8X
  142.  
  143. 	[int]$_Cardbus
  144.  
  145. 	[int]$_CDROMs
  146.  
  147. 	[int]$_COMPorts
  148.  
  149. 	[int]$_EISA
  150.  
  151. 	[int]$_HDDs
  152.  
  153. 	[int]$_ISA
  154.  
  155. 	[int]$_LPTPorts
  156.  
  157. 	[int]$_MemoryGB
  158.  
  159. 	[int]$_MemoryModules
  160.  
  161. 	[int]$_MemorySockets
  162.  
  163. 	[int]$_Monitors
  164.  
  165. 	[int]$_PCI
  166.  
  167. 	[int]$_PCIE
  168.  
  169. 	[int]$_PCIX
  170.  
  171. 	[int]$_PCMCIA
  172.  
  173. 	[int]$_USB2
  174.  
  175. 	[int]$_USB3
  176.  
  177. 	[int]$_VESA
  178.  
  179. 	Computer( )
  180. 	{
  181. 		$this.BIOS           = $( Get-CimInstance -ClassName Win32_BIOS )
  182. 		$this.CDROM          = $( Get-CimInstance -ClassName Win32_CDROMDrive | Where-Object { $_.Name -notmatch 'virtual' } )
  183. 		$this.CPU            = $( Get-CimInstance -ClassName Win32_Processor )
  184. 		$this.HDD            = $( Get-CimInstance -ClassName MSFT_PhysicalDisk -Namespace root/Microsoft/Windows/Storage )
  185. 		$this.Keyboard       = $( Get-CimInstance -ClassName Win32_Keyboard )
  186. 		$this.MainBoard      = $( Get-CimInstance -ClassName Win32_BaseBoard )
  187. 		$this.Memory         = $( Get-CimInstance -ClassName Win32_PhysicalMemory )
  188. 		$this.Monitor        = $( Get-CimInstance -ClassName WmiMonitorID -Namespace root/WMI )
  189. 		$this.Mouse          = $( Get-CimInstance -Classname Win32_PointingDevice )
  190. 		$this.NIC            = $( Get-CimInstance -ClassName Win32_NetworkAdapter | Where-Object { $_.Speed -gt 0 } ) # physical adapters only
  191. 		$this.Printers       = $( Get-CimInstance -ClassName Win32_Printer        | Where-Object { $_.PrintProcessor -ne 'winprint' -and $_.Local } ) # physical local printers only
  192. 		$this.Slots          = $( Get-CimInstance -ClassName Win32_SystemSlot )
  193. 		$this.Sound          = $( Get-CimInstance -ClassName Win32_SoundDevice    | Where-Object { $_.Manufacturer -ne 'Microsoft' } ) # physical soundcards only
  194. 		$this.USB            = $( Get-CimInstance -ClassName Win32_USBController )
  195. 		$this.Video          = $( Get-CimInstance -ClassName Win32_VideoController )
  196. 		$this.Windows        = $( Get-CimInstance -ClassName Win32_OperatingSystem )
  197. 		$this.WinSat         = $( Get-CimInstance -ClassName Win32_WinSAT -Filter "TimeTaken='MostRecentAssessment'" )
  198. 		$this._AGP           = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 73 } | Measure-Object ).Count
  199. 		$this._AGP2X         = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 81 } | Measure-Object ).Count
  200. 		$this._AGP4X         = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 82 } | Measure-Object ).Count
  201. 		$this._AGP8X         = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 99 } | Measure-Object ).Count
  202. 		$this._Cardbus       = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 52 } | Measure-Object ).Count
  203. 		$this._CDROMs        = $( $this.CDROM   | Measure-Object ).Count
  204. 		$this._COMPorts      = $( Get-CimInstance -ClassName Win32_SerialPort | Measure-Object ).Count
  205. 		$this._EISA          = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 45 } | Measure-Object ).Count
  206. 		$this._HDDs          = $( $this.HDD     | Measure-Object ).Count
  207. 		$this._ISA           = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 44 } | Measure-Object ).Count
  208. 		$this._LPTPorts      = $( Get-CimInstance -ClassName Win32_ParallelPort | Measure-Object ).Count
  209. 		$_Memory = 0
  210. 		$this.Memory.Capacity | ForEach-Object { $_Memory += $_ }
  211. 		$this._MemoryGB      = $( $_Memory / 1GB )
  212. 		$this._MemoryModules = $( $this.Memory  | Measure-Object ).Count
  213. 		$this._MemorySockets = $( Get-CimInstance -ClassName Win32_PhysicalMemoryArray ).MemoryDevices
  214. 		$this._Monitors      = $( $this.Monitor | Measure-Object ).Count
  215. 		$this._PCMCIA        = $( $this.Slots   | Where-Object { $_.ConnectorType -gt 46 -and $_.ConnectorType -lt 51 } | Measure-Object ).Count
  216. 		$this._PCI           = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 43 -or $_.ConnectorType -eq 80 } | Measure-Object ).Count
  217. 		$this._PCIE          = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 100 } | Measure-Object ).Count
  218. 		$this._PCIX          = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 88 } | Measure-Object ).Count
  219. 		$this._USB3          = $( $this.USB     | Where-Object { $_.Name -match 'USB ?3' } | Measure-Object ).Count # not very reliable, as this relies on the description
  220. 		$this._USB2          = $( $this.USB     | Measure-Object ).Count - $this._USB3 # total number of USB controllers - number of USB 3 controllers
  221. 		$this._VESA          = $( $this.Slots   | Where-Object { $_.ConnectorType -eq 46 } | Measure-Object ).Count
  222. 	}
  223. }
  224.  

page last uploaded: 2021-01-27