CPU Temp Powershell Script 1

 

So I’m back on posting some scripts again!.
This is one (of many) script(s) I started ages ago but never got around to finishing and today its finally done.

Using a library provided by Open Hardware Monitor I was able to create a PowerShell script that would get the actual temperatures of CPU cores.
If you would like to see a list of the TJMax values you can find it here

NOTE: Increase the Script execution time to 150 seconds just to allow the download of OHM on the first run.

CPU Temp Powershell Script 2

As always, if you would like to buy me a beer you can do so using this link.

paypal donate button[5]

 

<#	
	.NOTES
	===========================================================================
	 Created on:   	19/02/2016 1:43 PM
	 Created by:   	Jake Paterrnoster (jake@www.jcpit.com.au)
	 Organization: 	JCPIT Support (www.jcpit.com.au)
	 Filename:     	CPUTempCheck.ps1
	===========================================================================
	.DESCRIPTION
		Powershell script to check the CPU tempreture in GFI Max
#>

#Max Degrees celsius to trigger alert
$TjMax = 65

clear-host

function PathByService($serviceName)
{
	# Find service and use path to locate files
	$ServiceObject = Get-WmiObject Win32_Service | Where-Object { $_.Name -eq $serviceName }
	
	if ($ServiceObject)
	{
		$ServicePath = Split-Path $ServiceObject.PathName.Replace('"', "") -Parent
		return $ServicePath
	}
	else
	{
		Write-Host "Cant find service $serviceName"
		return $null
	}
}

# Find "Advanced Monitoring Agent" service and use path to locate files
$MaxPath = PathByService("Advanced Monitoring Agent")

# If We found the AMA folder
if ($MaxPath -ne $null)
{
	#If OpenHardwareMonitor not found, Download and extract the zip
	if (-not (Test-Path "$MaxPath\OpenHardwareMonitor\OpenHardwareMonitorLib.dll"))
	{
		(New-Object Net.WebClient).DownloadFile('http://openhardwaremonitor.org/files/openhardwaremonitor-v0.7.1-beta.zip', "$MaxPath\openhardwaremonitor.zip"); (new-object -com shell.application).namespace("$MaxPath").CopyHere((new-object -com shell.application).namespace("$MaxPath\openhardwaremonitor.zip").Items(), 16)
	}
	
	#Load the OpenHardwareMonitor Library
	[System.Reflection.Assembly]::LoadFile("$MaxPath\OpenHardwareMonitor\OpenHardwareMonitorLib.dll") | Out-Null
		
	$OverMax = $false
	$CoreCount = 0
	$PC = New-Object OpenHardwareMonitor.Hardware.Computer
	$PC.CPUEnabled = $true
	$PC.Open()
	
	ForEach ($hw in $PC.Hardware)
	{
		If ($hw.HardwareType -eq "CPU")
		{
			$hw.Name
			
			ForEach ($sensor in $hw.Sensors)
			{
				if ($sensor.SensorType -like "*Temp*")
				{
					if ($sensor.Value)
					{
						if ($sensor.Value -gt $TjMax)
						{
							$OverMax = $true
						}
						
						Write-Host "Core $CoreCount Temp:" $sensor.Value
						$CoreCount++
					}					
				}
			}
		}
	}
	
	If ($OverMax)
	{
		Exit 2000
	}
	Else
	{
		Exit 0
	}
}
Else
{
	Write-Host "Agent Path not found!"
	Exit 2000
}