Client Management IT Powershell

Check if Hibernation works

If you’re not sure if Hibernation works as planned I might have created a Solution for you!

A Script that monitors all interaction of a User with a Notebook. Starting the Script will set a counter to T + 15 Minutes (or whatever you configure) if no one touches the Device and the Device is set to Hibernate after 15 Minutes the Script should log the Hibernation Process and proof that Hibernation is working.

<#	
	.NOTES
	===========================================================================
	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.140
	===========================================================================
	.DESCRIPTION
		This Script monitors the Interaction of the User via Mouse, Keyboard and 
		Touch with the Notebook. If any interaction happened the Timer will be reset. 
		The Script should show you if Hibernation works as planned! 
#>

Write-Host "This Script is monitoring all Interaction with the Device, do not enter Passwords while this Script is running. Every Keypress made on this Device will be saved in Clear-Text!" -foregroundcolor red -backgroundcolor yellow
#<-=create Information about Start and End of Script
#Saving the Script Start Moment
$ScriptStart = get-date
#Setting the maximum Script time to 60 Minutes after Start
$ScriptEnd = $ScriptStart.AddMinutes(60)
#Setting the Last Interaction Time to Script start
$LastInteraction = $ScriptStart
#Calculating the planned Hibernation time if no Interaction Happens
$plannedHibernation = $LastInteraction.AddMinutes(15)
#Creating a variable for the Minute the Script has been startet for later use
$minute = $ScriptStart.Minute

#<-=Preparing Keypress Observation
#Importing user32.dll
$signature = @"
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern short GetAsyncKeyState(int virtualKeyCode);
"@
#Creating the $getKeyState method
$getKeyState = Add-Type -memberDefinition $signature -name "Newtype" -namespace newnamespace -passThru

#Preparing Mouse observation
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

#<-=Preparing Log
#Defining Log Path
$LogPath = "C:\TEMP\HibernationObserver.log"
#Testing path to LogFile and creating it if it not exists
if (!(Test-Path -Path $LogPath))
{
	New-Item -Type File -Path $LogPath
}
#Writing the first line into Log to show the Script has started
Add-Content -Path $LogPath -Value ($ScriptStart.ToString("s") + " - <-=Script has been started=->")

#While the current time ist lower than the end Time the Loop runs
While ((Get-Date) -lt $ScriptEnd)
{
	#Saving the intial LastInteraction time for later use
	$IAS = $LastInteraction
	#Save Mouse Position 1
	$p1 = [System.Windows.Forms.Cursor]::Position
	
	#Wait for 40 Milliseconds 
	Start-Sleep -Milliseconds 40
	#Every key (on a Standard Keybaord) has a Number from 1 to 254 this loop checks every Key 
	for ($char = 1; $char -le 254; $char++)
	{
		#Read out State of current Key ($char) and write State into $logged
		$logged = $getKeyState::GetAsyncKeyState($char)
		#if $logged equals 32767 the Key is pressed 
		if ($logged -eq -32767)
		{
			#Resetting the LastInteraction to recalculate planned Hibernation Time
			$LastInteraction = Get-Date
			#Replacing Numbers with the matching Charakter
			switch ($char)
			{
				65 { $letter = "a"; break }
				66 { $letter = "b"; break }
				67 { $letter = "c"; break }
				68 { $letter = "d"; break }
				69 { $letter = "e"; break }
				70 { $letter = "f"; break }
				71 { $letter = "g"; break }
				72 { $letter = "h"; break }
				73 { $letter = "i"; break }
				74 { $letter = "j"; break }
				75 { $letter = "k"; break }
				76 { $letter = "l"; break }
				77 { $letter = "m"; break }
				78 { $letter = "n"; break }
				79 { $letter = "o"; break }
				80 { $letter = "p"; break }
				81 { $letter = "q"; break }
				82 { $letter = "r"; break }
				83 { $letter = "s"; break }
				84 { $letter = "t"; break }
				85 { $letter = "u"; break }
				86 { $letter = "v"; break }
				87 { $letter = "w"; break }
				88 { $letter = "x"; break }
				89 { $letter = "y"; break }
				90 { $letter = "z"; break }
				220	{ $letter = "ä"; break }
				222	{ $letter = "ö"; break }
				186	{ $letter = "ü"; break }
				#If the Number isn't one of the Characters its the Special key -> Writing the Number to the Log
				default { $letter = $char }
			}
			#Informing the user on console about the Keypress
			Write-Host ($LastInteraction.ToString("s") + " - KeyPressed: " + $letter)
			#Logging the Keypress to Logfile
			Add-Content -Path $LogPath -Value ($LastInteraction.ToString("s") + " - KeyPressed: " + $letter)
		}
	}
	#Save Mouse Position 2
	$p2 = [System.Windows.Forms.Cursor]::Position
	
	#If mouse position2 is not like mouse position2 then the Mouse has moved
	if ($p1.X -ne $p2.X -or $p1.Y -ne $p2.Y)
	{
		#Resetting the LastInteraction to recalculate planned Hibernation Time
		$LastInteraction = Get-Date
		#Informing the User on console about the Mouse movement
		Write-Host ($LastInteraction.ToString("s") + " - Mouse Moved!")
		#Logging the Mouse Movement to Logfile
		Add-Content -Path $LogPath -Value ($LastInteraction.ToString("s") + " - Mouse Moved!")
	}
	#Checking if LastInteraction has been changed since start of the Script
	if ($LastInteraction -ne $IAS)
	{
		#Informing the User on Console the Interaction has been noticed
		Write-Host ($LastInteraction.ToString("s") + " Interaction Happened")
		#Recalculating Hibernation Time
		$plannedHibernation = $LastInteraction.AddMinutes(15)
		#Logging new planned Hibernation time to Logfile
		Add-Content -Path $LogPath -Value ($LastInteraction.ToString("s") + " - Interaction Happened, planned Hibernation is now: " + $plannedHibernation.ToString("s"))
	}
	#Checking if a Minute has past since last Minute-Log
	if ((get-date).Minute -ne $minute)
	{
		#Resetting the Minute counter to current minute
		$minute = (Get-Date).Minute
		#Logging the Status Message to Logfile
		Add-Content -Path $LogPath -Value ((get-date).ToString("s") + " - A Minute passed, planned Hibernation is: " + $plannedHibernation.ToString("s") + " wich is in " + ($plannedHibernation.Minute - (get-date).Minute ) )
		#Informing the Console User about the new Minute Status
		Write-Host ((get-date).ToString("s") + " - A Minute passed, planned Hibernation is: " + $plannedHibernation.ToString("s") + " wich is in " + ($plannedHibernation.Minute - (get-date).Minute) )	
	}
}