Private/class.HardwareMonitor.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
#Hardware Monitoring class HardwareMonitor { #properties #[System.Management.Automation.Runspaces.PSSession] $RemoteSession [System.Collections.ArrayList] $HardwareMonitorLog [String] $LastErrorMessage [String]$OpenHWMonitoringDLLVersion [Bool] $EnableCPU = $true [Bool] $EnableGPU = $false [System.Object]$Temperatures hidden [string] $HardwareMonitorDLLPath = "\DLL\OpenHardwareMonitorLib.dll" [System.Object] $PCHARDWARE #Constructor HardwareMonitor(){ $this.CheckHardwareMonitoringDLL() } hidden [void] CheckHardwareMonitoringDLL() { $this.InitialLog() try{ Get-Item -Path "$($PSScriptRoot)$($this.HardwareMonitorDLLPath)" -ErrorAction Stop $this.OpenHWMonitoringDLLVersion=(Get-Item -Path "$($PSScriptRoot)$($this.HardwareMonitorDLLPath)").VersionInfo.FileVersion } catch{ $this.DoLog("Error","Hardware Monitoring File was not Found") throw $_.Exception.Message } try{ Get-Item -Stream Zone.Identifier -Path "$($PSScriptRoot)$($this.HardwareMonitorDLLPath)" -ErrorAction Stop Unblock-File "$($PSScriptRoot)$($this.HardwareMonitorDLLPath)" } catch{ $this.DoLog("Information","File was tested on Zone.Identifier and was clear") $this.InitialHardWareMonitoringObject() } }#End Constructor hidden [void] InitialLog(){ $this.HardwareMonitorLog = New-Object System.Collections.ArrayList }#End Initial Log [void] DoLog($LogLevel,$LogMessage){ $LogEntry = [pscustomobject]@{ Time=(Get-Date) Level= $LogLevel Message= $LogMessage } switch ($Loglevel) { "Error" { $this.LastErrorMessage = "$($logentry.Time) | $($LogEntry.Message)" $this.HardwareMonitorLog.Add($LogEntry) } Default {$this.HardwareMonitorLog.Add($LogEntry)} } }#End Log hidden [void] InitialHardWareMonitoringObject(){ try{ [System.Reflection.Assembly]::LoadFile("$($PSScriptRoot)$($this.HardwareMonitorDLLPath)") | Out-Null $this.PCHARDWARE = New-Object OpenHardwareMonitor.Hardware.Computer $this.PCHARDWARE.open() $this.DoLog("Information","Class OpenHardwareMonitor Loaded") } catch{ $this.DoLog("Error","Could not load OpenHardwareMonitorLib.dll") throw $_.Exception.Message } }#End Initial .Net Class Hwardware monitoring hidden [void] SetMeasuremetComponents(){ $this.PCHARDWARE.CPUEnabled = $this.EnableCPU $this.PCHARDWARE.GPUEnabled = $this.EnableGPU } [System.Object] GetMeasurementValues(){ $this.SetMeasuremetComponents() $this.PCHARDWARE.Hardware.Update() $this.Temperatures = $this.PCHARDWARE.Hardware.Sensors | Select-Object SensorType,Name,Index,Min,Max,Value | Where-Object {$_.SensorType -eq "Temperature"} return $this.Temperatures } } #Class End |