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.

2
  • 1
    Possible duplicate of Powershell Log Off Remote Session Commented Jul 2, 2016 at 21:36
  • I will give that a shot thank you! Commented Jul 6, 2016 at 19:48

1 Answer 1

0

Have you tried with the command qwinsta?

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.