{"id":371,"date":"2017-11-02T17:31:49","date_gmt":"2017-11-02T16:31:49","guid":{"rendered":"http:\/\/thinkadvantage.ch\/?p=371"},"modified":"2017-11-02T17:31:49","modified_gmt":"2017-11-02T16:31:49","slug":"check-if-hibernation-works","status":"publish","type":"post","link":"https:\/\/www.raphaeleymann.ch\/?p=371","title":{"rendered":"Check if Hibernation works"},"content":{"rendered":"<p>If you&#8217;re not sure if Hibernation works as planned I might have created a Solution for you! <\/p>\n<p>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. <\/p>\n<p>[sourcecode language=&#8221;powershell&#8221;]<br \/>\n&lt;#<br \/>\n\t.NOTES<br \/>\n\t===========================================================================<br \/>\n\t Created with: \tSAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.140<br \/>\n\t===========================================================================<br \/>\n\t.DESCRIPTION<br \/>\n\t\tThis Script monitors the Interaction of the User via Mouse, Keyboard and<br \/>\n\t\tTouch with the Notebook. If any interaction happened the Timer will be reset.<br \/>\n\t\tThe Script should show you if Hibernation works as planned!<br \/>\n#&gt;<\/p>\n<p>Write-Host &quot;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!&quot; -foregroundcolor red -backgroundcolor yellow<br \/>\n#&lt;-=create Information about Start and End of Script<br \/>\n#Saving the Script Start Moment<br \/>\n$ScriptStart = get-date<br \/>\n#Setting the maximum Script time to 60 Minutes after Start<br \/>\n$ScriptEnd = $ScriptStart.AddMinutes(60)<br \/>\n#Setting the Last Interaction Time to Script start<br \/>\n$LastInteraction = $ScriptStart<br \/>\n#Calculating the planned Hibernation time if no Interaction Happens<br \/>\n$plannedHibernation = $LastInteraction.AddMinutes(15)<br \/>\n#Creating a variable for the Minute the Script has been startet for later use<br \/>\n$minute = $ScriptStart.Minute<\/p>\n<p>#&lt;-=Preparing Keypress Observation<br \/>\n#Importing user32.dll<br \/>\n$signature = @&quot;<br \/>\n[DllImport(&quot;user32.dll&quot;, CharSet=CharSet.Auto, ExactSpelling=true)]<br \/>\npublic static extern short GetAsyncKeyState(int virtualKeyCode);<br \/>\n&quot;@<br \/>\n#Creating the $getKeyState method<br \/>\n$getKeyState = Add-Type -memberDefinition $signature -name &quot;Newtype&quot; -namespace newnamespace -passThru<\/p>\n<p>#Preparing Mouse observation<br \/>\n[void][System.Reflection.Assembly]::LoadWithPartialName(&quot;System.Windows.Forms&quot;)<\/p>\n<p>#&lt;-=Preparing Log<br \/>\n#Defining Log Path<br \/>\n$LogPath = &quot;C:\\TEMP\\HibernationObserver.log&quot;<br \/>\n#Testing path to LogFile and creating it if it not exists<br \/>\nif (!(Test-Path -Path $LogPath))<br \/>\n{<br \/>\n\tNew-Item -Type File -Path $LogPath<br \/>\n}<br \/>\n#Writing the first line into Log to show the Script has started<br \/>\nAdd-Content -Path $LogPath -Value ($ScriptStart.ToString(&quot;s&quot;) + &quot; &#8211; &lt;-=Script has been started=-&gt;&quot;)<\/p>\n<p>#While the current time ist lower than the end Time the Loop runs<br \/>\nWhile ((Get-Date) -lt $ScriptEnd)<br \/>\n{<br \/>\n\t#Saving the intial LastInteraction time for later use<br \/>\n\t$IAS = $LastInteraction<br \/>\n\t#Save Mouse Position 1<br \/>\n\t$p1 = [System.Windows.Forms.Cursor]::Position<\/p>\n<p>\t#Wait for 40 Milliseconds<br \/>\n\tStart-Sleep -Milliseconds 40<br \/>\n\t#Every key (on a Standard Keybaord) has a Number from 1 to 254 this loop checks every Key<br \/>\n\tfor ($char = 1; $char -le 254; $char++)<br \/>\n\t{<br \/>\n\t\t#Read out State of current Key ($char) and write State into $logged<br \/>\n\t\t$logged = $getKeyState::GetAsyncKeyState($char)<br \/>\n\t\t#if $logged equals 32767 the Key is pressed<br \/>\n\t\tif ($logged -eq -32767)<br \/>\n\t\t{<br \/>\n\t\t\t#Resetting the LastInteraction to recalculate planned Hibernation Time<br \/>\n\t\t\t$LastInteraction = Get-Date<br \/>\n\t\t\t#Replacing Numbers with the matching Charakter<br \/>\n\t\t\tswitch ($char)<br \/>\n\t\t\t{<br \/>\n\t\t\t\t65 { $letter = &quot;a&quot;; break }<br \/>\n\t\t\t\t66 { $letter = &quot;b&quot;; break }<br \/>\n\t\t\t\t67 { $letter = &quot;c&quot;; break }<br \/>\n\t\t\t\t68 { $letter = &quot;d&quot;; break }<br \/>\n\t\t\t\t69 { $letter = &quot;e&quot;; break }<br \/>\n\t\t\t\t70 { $letter = &quot;f&quot;; break }<br \/>\n\t\t\t\t71 { $letter = &quot;g&quot;; break }<br \/>\n\t\t\t\t72 { $letter = &quot;h&quot;; break }<br \/>\n\t\t\t\t73 { $letter = &quot;i&quot;; break }<br \/>\n\t\t\t\t74 { $letter = &quot;j&quot;; break }<br \/>\n\t\t\t\t75 { $letter = &quot;k&quot;; break }<br \/>\n\t\t\t\t76 { $letter = &quot;l&quot;; break }<br \/>\n\t\t\t\t77 { $letter = &quot;m&quot;; break }<br \/>\n\t\t\t\t78 { $letter = &quot;n&quot;; break }<br \/>\n\t\t\t\t79 { $letter = &quot;o&quot;; break }<br \/>\n\t\t\t\t80 { $letter = &quot;p&quot;; break }<br \/>\n\t\t\t\t81 { $letter = &quot;q&quot;; break }<br \/>\n\t\t\t\t82 { $letter = &quot;r&quot;; break }<br \/>\n\t\t\t\t83 { $letter = &quot;s&quot;; break }<br \/>\n\t\t\t\t84 { $letter = &quot;t&quot;; break }<br \/>\n\t\t\t\t85 { $letter = &quot;u&quot;; break }<br \/>\n\t\t\t\t86 { $letter = &quot;v&quot;; break }<br \/>\n\t\t\t\t87 { $letter = &quot;w&quot;; break }<br \/>\n\t\t\t\t88 { $letter = &quot;x&quot;; break }<br \/>\n\t\t\t\t89 { $letter = &quot;y&quot;; break }<br \/>\n\t\t\t\t90 { $letter = &quot;z&quot;; break }<br \/>\n\t\t\t\t220\t{ $letter = &quot;\u00e4&quot;; break }<br \/>\n\t\t\t\t222\t{ $letter = &quot;\u00f6&quot;; break }<br \/>\n\t\t\t\t186\t{ $letter = &quot;\u00fc&quot;; break }<br \/>\n\t\t\t\t#If the Number isn&#8217;t one of the Characters its the Special key -&gt; Writing the Number to the Log<br \/>\n\t\t\t\tdefault { $letter = $char }<br \/>\n\t\t\t}<br \/>\n\t\t\t#Informing the user on console about the Keypress<br \/>\n\t\t\tWrite-Host ($LastInteraction.ToString(&quot;s&quot;) + &quot; &#8211; KeyPressed: &quot; + $letter)<br \/>\n\t\t\t#Logging the Keypress to Logfile<br \/>\n\t\t\tAdd-Content -Path $LogPath -Value ($LastInteraction.ToString(&quot;s&quot;) + &quot; &#8211; KeyPressed: &quot; + $letter)<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\t#Save Mouse Position 2<br \/>\n\t$p2 = [System.Windows.Forms.Cursor]::Position<\/p>\n<p>\t#If mouse position2 is not like mouse position2 then the Mouse has moved<br \/>\n\tif ($p1.X -ne $p2.X -or $p1.Y -ne $p2.Y)<br \/>\n\t{<br \/>\n\t\t#Resetting the LastInteraction to recalculate planned Hibernation Time<br \/>\n\t\t$LastInteraction = Get-Date<br \/>\n\t\t#Informing the User on console about the Mouse movement<br \/>\n\t\tWrite-Host ($LastInteraction.ToString(&quot;s&quot;) + &quot; &#8211; Mouse Moved!&quot;)<br \/>\n\t\t#Logging the Mouse Movement to Logfile<br \/>\n\t\tAdd-Content -Path $LogPath -Value ($LastInteraction.ToString(&quot;s&quot;) + &quot; &#8211; Mouse Moved!&quot;)<br \/>\n\t}<br \/>\n\t#Checking if LastInteraction has been changed since start of the Script<br \/>\n\tif ($LastInteraction -ne $IAS)<br \/>\n\t{<br \/>\n\t\t#Informing the User on Console the Interaction has been noticed<br \/>\n\t\tWrite-Host ($LastInteraction.ToString(&quot;s&quot;) + &quot; Interaction Happened&quot;)<br \/>\n\t\t#Recalculating Hibernation Time<br \/>\n\t\t$plannedHibernation = $LastInteraction.AddMinutes(15)<br \/>\n\t\t#Logging new planned Hibernation time to Logfile<br \/>\n\t\tAdd-Content -Path $LogPath -Value ($LastInteraction.ToString(&quot;s&quot;) + &quot; &#8211; Interaction Happened, planned Hibernation is now: &quot; + $plannedHibernation.ToString(&quot;s&quot;))<br \/>\n\t}<br \/>\n\t#Checking if a Minute has past since last Minute-Log<br \/>\n\tif ((get-date).Minute -ne $minute)<br \/>\n\t{<br \/>\n\t\t#Resetting the Minute counter to current minute<br \/>\n\t\t$minute = (Get-Date).Minute<br \/>\n\t\t#Logging the Status Message to Logfile<br \/>\n\t\tAdd-Content -Path $LogPath -Value ((get-date).ToString(&quot;s&quot;) + &quot; &#8211; A Minute passed, planned Hibernation is: &quot; + $plannedHibernation.ToString(&quot;s&quot;) + &quot; wich is in &quot; + ($plannedHibernation.Minute &#8211; (get-date).Minute ) )<br \/>\n\t\t#Informing the Console User about the new Minute Status<br \/>\n\t\tWrite-Host ((get-date).ToString(&quot;s&quot;) + &quot; &#8211; A Minute passed, planned Hibernation is: &quot; + $plannedHibernation.ToString(&quot;s&quot;) + &quot; wich is in &quot; + ($plannedHibernation.Minute &#8211; (get-date).Minute) )<br \/>\n\t}<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,9,30],"tags":[29,31,32],"class_list":["post-371","post","type-post","status-publish","format-standard","hentry","category-clientmanagement","category-it","category-powershell","tag-client-management","tag-powershell","tag-sccm"],"_links":{"self":[{"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=\/wp\/v2\/posts\/371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=371"}],"version-history":[{"count":0,"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=\/wp\/v2\/posts\/371\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.raphaeleymann.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}