Skip to content

Commit

Permalink
Version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Willa committed Apr 18, 2020
1 parent 29956ad commit 6a2903a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 114 deletions.
Binary file modified DLL/OpenHardwareMonitorLib.dll
Binary file not shown.
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# PSHardwareMonitoring

This Powershell Class catch the min/max/current value from the CPU and GPU temperature. Load is comming...
This Powershell Class catch the min/max/current value from the CPU and GPU temperature

## Getting Started
Copy the ps1 and dll to your Computer:

```
|DIR
|class.PSHardwareMonitor.ps1
|-DLL
|--OpenHardwareMonitorLib.dll
```
Clone this Repo

**Load the class**
```
Expand All @@ -24,26 +17,31 @@ $HARDWAREMONITOR = [HardwareMonitor]::new()

**Set required Parameters**
```
$HARDWAREMONITOR.ComputerName = "COMPUTERNAME"
$HARDWAREMONITOR.EnableCPU = $true|$false {default: $True}
$HARDWAREMONITOR.EnableGPU = $true|$false {default: $False}
```

**Get Value from System**
```
$HARDWAREMONITOR.GetHardwareValuesFromReomtePC()
$HARDWAREMONITOR.GetMeasurementValues()
```

**Show Value from System**
```
$HARDWAREMONITOR.Values()
$HARDWAREMONITOR.Temperatures()
```

**Destroy Session**
**Show Logging
```
$HARDWAREMONITOR.DestroyRemoteSession()
$HARDWAREMONITOR.HardwareMonitorLog
```

**Show Last Error
```
$HARDWAREMONITOR.LastErrorMessage
```


### Prerequisites

What things you need to install the software and how to install them
Expand Down
178 changes: 78 additions & 100 deletions class.HardwareMonitor.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,130 +3,108 @@
class HardwareMonitor {

#properties
[String] $Computername
[Bool] $OnlineState
[Bool] $OpenHardwareMonitorDLL

#[System.Management.Automation.Runspaces.PSSession] $RemoteSession
[System.Collections.ArrayList] $HardwareMonitorLog
[String] $LastErrorMessage
[String]$OpenHWMonitoringDLLVersion
[Bool] $EnableCPU = $true
[Bool] $EnableGPU = $false
[System.Management.Automation.Runspaces.PSSession] $RemoteSession
[System.Object]$Values
[datetime]$TimeStamp
[String] $LastInfoMessage
[String] $LastErrorMessage
[System.Object]$Temperatures
hidden [string] $HardwareMonitorDLLPath = "\DLL\OpenHardwareMonitorLib.dll"
[System.Object] $PCHARDWARE



#Constructor
HardwareMonitor(){
}

#Test OnlineState
[void] TestOnlineState (){
if (Test-Connection -Server $this.Computername -Count 1 -ea SilentlyContinue){
$this.OnlineState = $true
$this.CheckHardwareMonitoringDLL()
}
else {
$this.OnlineState = $false
}
}

#Build Remote PS Session
[void] BuildRemoteSession (){

$this.TestOnlineState()
If ($this.OnlineState -eq $true){
$this.RemoteSession = New-PsRemoteSession -ComputerName $this.Computername
}
else{
$this.LastErrorMessage = "no conection established"
}
}

#Destroy Remote Sessin
[void] DestroyRemoteSession (){
Remove-PSSession $this.RemoteSession
$this.RemoteSession = $null
$this.OpenHardwareMonitorDLL = $false
}

#Test HardwareMonitorDLL
[void] TestHardwareMonitorDLL (){
$DLLPath = (Get-Item -Path ".\").FullName + "\DLL\OpenHardwareMonitorLib.dll"
$Command = {Test-Path -Path "C:\Script\PSHardwareMonitor"}
hidden [void] CheckHardwareMonitoringDLL()
{
$this.InitialLog()

if (Invoke-Command -Session $this.RemoteSession -ScriptBlock $Command){
$this.OpenHardwareMonitorDLL = $true
}
else{
$Command = {new-item -type directory -force "c:\Script\PSHardwareMonitor\"}
Invoke-Command -Session $this.RemoteSession -ScriptBlock $Command
Copy-Item $DLLPath -ToSession $this.RemoteSession -Destination "C:\Script\PSHardwareMonitor\" -Recurse -Force
#Remove Security Trust download from Internet
$command = {Unblock-File -Path "C:\Script\PSHardwareMonitor\OpenHardwareMonitorLib.dll"}
Invoke-Command -Session $this.RemoteSession -ScriptBlock $Command
$this.LastInfoMessage = "Open Hardware Monitoring DLL not exsist -> copied"
$this.TestHardwareMonitorDLL()
}
}

#Build OpenDLL Object
[void] BuildOpenDLLObject(){

#Parameter Bindings
$CPUEnable=$this.EnableCPU
$GPUEnable=$this.EnableGPU

$command = {
param($CPUEnable, $GPUEnable)
[System.Reflection.Assembly]::LoadFile("C:\Script\PSHardwareMonitor\OpenHardwareMonitorLib.dll") | Out-Null
$PCHARDWARE = New-Object OpenHardwareMonitor.Hardware.Computer
$PCHARDWARE.CPUEnabled = $CPUEnable
$PCHARDWARE.GPUEnabled = $GPUEnable
$PCHARDWARE.Open()
}
Invoke-Command -Session $this.RemoteSession -ScriptBlock $Command -ArgumentList ($CPUEnable, $GPUEnable)
}
try{
Get-Item -Path "$($PSScriptRoot)$($this.HardwareMonitorDLLPath)" -ErrorAction Stop
$this.OpenHWMonitoringDLLVersion=(Get-Item .\DLL\OpenHardwareMonitorLib.dll).VersionInfo.FileVersion
}

# GetHardwareValues from Session
[void] GetHardwareValuesFromSession (){

#Get CPU Value
catch{
$this.DoLog("Error","Hardware Monitoring File was not Found")
throw $_.Exception.Message
}

$command = {
$PCHARDWARE.Hardware.Update()
$PCHARDWARE.Hardware.Sensors | Select-Object SensorType,Name,Index,Min,Max,Value | Where-Object {$_.SensorType -eq "Temperature"}
try{
Get-Item -Stream Zone.Identifier -Path "$($PSScriptRoot)$($this.HardwareMonitorDLLPath)" -ErrorAction Stop
Unblock-File "$($PSScriptRoot)$($this.HardwareMonitorDLLPath)"
}
$this.Values = Invoke-Command -Session $this.RemoteSession -ScriptBlock $command

catch{
$this.DoLog("Information","File was tested on Zone.Identifier and was clear")
$this.InitialHardWareMonitoringObject()


$this.TimeStamp = get-date -Format "dd/MM/yyyy HH:mm:ss"
}

# GetHardwareValuesFromRemotePC
[void] GetHardwareValuesFromReomtePC (){
$this.TestOnlineState()

if ($this.OnlineState -eq $true){
}
}#End Constructor

#Test If Remotesession is Open
if ($this.RemoteSession -eq $null){
$this.BuildRemoteSession()
}

#Test If DLL exsist
If ($this.OpenHardwareMonitorDLL -eq $false){
$this.TestHardwareMonitorDLL()
$this.BuildOpenDLLObject()
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")
}
$this.GetHardwareValuesFromSession()
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

0 comments on commit 6a2903a

Please sign in to comment.