I have already written a PowerShell Mouse Jiggler script. Today, right now, we will go through it, breaking it apart and explaining the how and why of this script.
Sometimes you need to keep your PC up and running even while you are away. This of course is a horrible security practice, however, we all know there are times where it needs to happen. The system, for whatever reason, just cannot go to sleep.
To start we will need to instantiate a Windows Form
. We do this so we can use the System.Windows.Forms.Cursor
in order to move the mouse later on.
Add-Type -AssemblyName System.Windows.Forms
Next, we will use a While
cmdlet. As a result, aWhile
cmdlet runs a command block based on the results of a conditional test. In the instance of While
, the conditional test needs to evaluate to true. It is important to remember this when using the While
cmdlet. This loops through the while
until a specific time that we define. In this case 5PM. We define $hour
to be 17 inside the While
loop. The number 17 indicates 5PM.
#While Loop Work while ($hour -le 17) { }
The above portion of the script says that While $hour is less than 17
we will loop through this every ten seconds.
Using Windows Forms
again we are able to move the cursor to a specific location. What we are going to do is move the mouse one pixel in both X
and Y
every 10 seconds. Finally, updating the $hour
variable with the current hour.
To do this we first grab the current position of the mouse cursor and throw that data in a variable we call $Pos
.
#Use Windows Forms to get the position of the Cursor, and store it in a variable called $Pos $Pos = [System.Windows.Forms.Cursor]::Position
Next, we reset the current mouse location to just 1 pixel away on X
and Y
, by doing the math first and placing the data inside the $x
, and $y
variables.
#Reset current mouse location $x = ($pos.X %% 500) + 1 $y = ($pos.Y %% 500) + 1
Now we apply the new location data we calculated above to the Windows Forms
controls in order to move the mouse.
#Apply New Position for mouse [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
Then the script waits 10 seconds then places the current hour in the $hour
variable. To do this we use Start-Sleep
and set it to. then we use Get-Date
with dot (.
) notation for the current hour.
#Wait 10 seconds Start-Sleep -Seconds 10 #Check to see what time it is. (Hour) $hour = (get-date).hour
Finally, adding the already-written code to hide the PowerShell console will clean this up and make it completely invisible. Sometimes a script can be as large as one line. Other times the script can be 2000 plus lines. The idea is, at least for me, if I have done it before I do not want to recreate that wheel. In that instance I have scripts like the one below that is used to do a specific task, and that is what I am using that already-written code.
# Make PowerShell Disappear $windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);' $asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru $null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0)
The final and complete script is here:
If you like this post check these out!