$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
echo $userID
$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)
}
What I am trying to do its log off all users in our conference rooms after a given idle time. What I am trying to do is find all session id's and log off all active sessions. I am not worried about losing work as these are conference room computers. The problem I have is that I can get the session id for the current logged in user but not for all the users logged in. If anyone has any insight it would be greatly appreciated.
here are some options that may be of use
powershell way
$computer = 'localhost'
$owners = @{}
Get-WmiObject win32_process -ComputerName $computer -Filter 'name = "explorer.exe"' | % {$owners[$_.handle] = $_.getowner().user}
Get-Process -ComputerName $computer explorer | % {$owners[$_.id.tostring()]}
or these
$server = 'localhost'
(quser /server:$server) -replace '\s{2,}', ',' | ConvertFrom-Csv
# IDENTICAL
query session /server:$server
qwinsta /server:$server
# IDENTICAL
query user /server:$server
quser /server:$server
qprocess explorer.exe /server:$server
you can better do it with remote desktop session host configuration idle time limit to your desired time.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With