Script/Routine to run program

Asked

Viewed 205 times

1

Hello.

I would like an example of Script by the same VBS, to check if there is activity in Windows and so that if there is no activity in the previous 15 minutes, this script runs a certain program.

  • define "activity" - your script running is already an activity!

  • windows activity, type mouse drive, or typing on keyboard, type this

1 answer

1

Tested in powershell 5.1, windows 10.

Add-Type @'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace PInvoke.Win32 {

    public static class UserInput {

        [DllImport("user32.dll", SetLastError=false)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        [StructLayout(LayoutKind.Sequential)]
        private struct LASTINPUTINFO {
            public uint cbSize;
            public int dwTime;
        }

        public static DateTime LastInput {
            get {
                DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
                DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);
                return lastInput;
            }
        }

        public static TimeSpan IdleTime {
            get {
                return DateTime.UtcNow.Subtract(LastInput);
            }
        }

        public static int LastInputTicks {
            get {
                LASTINPUTINFO lii = new LASTINPUTINFO();
                lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
                GetLastInputInfo(ref lii);
                return lii.dwTime;
            }
        }
    }
}
'@

Example of use:

For ( $i = 0; $i -lt 10; $i++ ) {
    Write-Host -Object 'Última entrada', ( [PInvoke.Win32.UserInput]::LastInput ).ToString( 'T' )
    Write-Host -Object 'Ociosidade atual', ( [PInvoke.Win32.UserInput]::IdleTime ).ToString( 'T' )
    Start-Sleep -Seconds ( Get-Random -Minimum 1 -Maximum 5 )
}

Source: https://stackoverflow.com/questions/15845508/get-idle-time-of-machine

You may not be able to test the 15 minutes exactly, because you would have to check every second and it will weigh on the machine processing.

Perhaps a 2 minute break to the "Start-Sleep" command would be interesting. Just test the idle time. If more than 15 minutes, perform some routine, using the "Start-Process command".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.