0
   $idle_timout = New-TimeSpan -minutes 1
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;
            }
        }
    }
}
'@

$userId = (Get-Process -PID $pid).SessionID
$loggedOff = 0

foreach ($user in $userid){
   do
{
    $idle_time = [PInvoke.Win32.UserInput]::IdleTime

    if (($loggedOff -eq 0) -And ($idle_time -gt $idle_timout))
    {
        logoff $user

        $loggedOff = 1
    }

    if ($idle_time -lt $idle_timout)
    {
        $loggedOff = 0
    }
}
while (1 -eq 1)

}

I am trying to write a script that will find the user PID of all logged in users for a particular computer and then forcefully log them off after a set amount of idle time. Write now I have that time set to 1 minute to test but ideally it would be 15 minutes and i would put this script into a scheduled task and have it run that way. What I am having trouble doing is finding the PID for all users logged in. Currently my script will find the PID of the currently logged in active user and log them out but i need the script to log everyone out. I read somewhere that I can search for explorer instances and then log off the users based on that. If anyone has more insight or help I would be very grateful. Thank you and may the uptime be with you.

GoldenWest
  • 281
  • 2
  • 4
  • 16

1 Answers1

0

Have you tried with the command qwinsta?

vinu
  • 81
  • 1
  • 1
  • 7
  • i will try that to query all sessions but from that i need to be able to pull the session id and log out that particular session with admin rights. the difficulty arises from having multiple users logged into the same machine. – GoldenWest Jul 06 '16 at 19:51