Script to keep Windows PC Active

Dec 03, 2020 1 min read
Script to keep Windows PC Active

If you are under a corporation's network which doesn't allow you to install 3rd party programs and also the screen times out after inactivity, you can use this script to keep the screen active which presses Tab every x amount of time.

If you are under a corporation's network which doesn't allow you to install 3rd party programs and also the screen times out after inactivity, you can use this script to keep the screen active which presses Tab every x amount of time.

Steps to use it:

  1. Create a Power Shell script (format .ps1)
  2. Open a new Power Shell window and drag the file, press enter and select the area you want the tab to be used, maybe a Firefox tab?

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

 

Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;

 

# https://blogs.technet.microsoft.com/heyscriptingguy/2015/12/26/weekend-scripter-manage-window-placement-by-using-pinvoke/

Add-Type @"

using System;

using System.Runtime.InteropServices;

public class myWindow {

    [DllImport("user32.dll")]

    [return: MarshalAs(UnmanagedType.Bool)]

    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

}

"@

 

$current_position = [System.Windows.Forms.Cursor]::Position

$x = [int]$current_position.X

$y = [int]$current_position.Y

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)

[W.U32]::mouse_event(6,0,0,0,0);

 

$check = 0

do{

    $msg = (Get-Process | Where-Object { $_.MainWindowTitle -like '*Google Chrome' }).MainWindowTitle

    try {

        $msg = [int]$msg.Substring(1,1)

        if ($msg){

            #Send-MailMessage -From '[email protected]' -To '[email protected]' -Subject "Someone is chatting with you" -Body 'Please reply back' -SmtpServer 'mail.mastec.com'

        }

    } catch{}

   Start-Sleep -s 15

   [System.Windows.Forms.SendKeys]::SendWait("^{TAB}")

} while($check -eq 0)
Great! Next, complete checkout for full access to ArturoFM.
Welcome back! You've successfully signed in.
You've successfully subscribed to ArturoFM.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.