PowerShell – Monitor CPU Cores Temperatures

October 19, 2019

In order to Monitor the Temperature of your CPU Cores it is best to make use of OpenHardwareMonitorLib

 

image

You can download it here.

 

This nice application has a .Net Library that you can use to access the Hardware Sensors Data

It also exposes the values to WMI when the GUI is started, but that is not so convenient for monitoring

 

SOLUTION :

# Needs admin privileges and the .NET OpenHardwareMonitorLib.dll

#Requires -RunAsAdministrator

CLS

Add-Type -Path "C:\OpenHardwareMonitor\OpenHardwareMonitorLib.dll"

$Comp = New-Object -TypeName OpenHardwareMonitor.Hardware.Computer

$Comp.Open()

$Comp.CPUEnabled = $true

$Comp.RAMEnabled = $true

$Comp.MainboardEnabled = $true

$Comp.FanControllerEnabled = $true

$Comp.GPUEnabled = $true

$Comp.HDDEnabled = $true

ForEach ($HW in $Comp.Hardware) {

$HW.Update()
    $hw.HardwareType.ToString() + ' - ' + $hw.name.ToString()

    If ( $hw.HardwareType -eq "CPU"){
        ForEach ($Sensor in $HW.Sensors) {

        If ($Sensor.SensorType -eq "Temperature"){
            
            $Sensor.Name + ' - Temp : ' + $Sensor.Value.ToString() + ' C - Min. : ' + $Sensor.Min.ToString() + ' C - Max : ' + $Sensor.Max.ToString() + ' C'
        }
      }
    }
   
    # $hw.Sensors
    $hw.SubHardware
}
$Comp.Close()

 

image

 

If you add an Email Notifications when it reaches the MAX values, you have a nice Monitoring System Smile

Enjoy !


PowerShell – Run Assembly that is not registered in the GAC

September 5, 2014

As demonstrated in previous posts you can easily compile your own .Net Assemblies.

Using AutoIT to create a Wrapper for compiling an EXE or DLL using .NET.

image

Or  your own COM objects

But now it is time to see once we compiled the Assembly.dll, and how we can run it in PowerShell. Without registering it the GAC !

Here is some example code to create a .Net assembly.dll

Public module HelloWorld

 Sub main()
 Console.WriteLine ("Hello World using Visual Basic Code!")
 Msgbox("Hello World from My DotNET Assembly !")
 End sub
End module

Save this code as a Hello World.vb file.

If you compile it using the above Wrapper, choose the Class Library button to generate the DLL.

Once this is successfully processed you will see the Assembly.dll that is created.

image

Using general .NET practice you would need to register the DLL to the GAC in order to access the Methods and Properties. But for scripters like us this is not practical Winking smile

So we want to distribute the DLL across PC’s and Servers by just copying the file around.

So this example shows you have to access the DLL from within PowerShell.

It pops up a message box and wrote something to the console

image

By the way since PS v3.0 the IDE you can use the intellisense feature on you newly created Assembly.dll

image

 

CLS
# Notice the double backslash !
$filename = "C:\\_\\Apps\\Hello World.dll"
$assembly = [Reflection.Assembly]::Loadfile($filename)

echo '--'
echo $assembly.gettype()
echo '--'
echo $assembly.fullName
echo '--'
echo $assembly.gettypes() | ?{$_.IsPublic} # List Public Functions
echo '--'

# Call the Class / Methods
[HelloWorld]::Main()

# Clean-up
rv assembly, filename

As simple as that, isn’t that great ! 🙂

So one more missing link to make the circle round. What if we could access the .NET Assembly from within Autoit running this PS code that would be just great.

Coming soon stay tuned !

Enjoy!