User Idle session time
Add-Type @"
using System;
using System.Runtime.InteropServices;
"@
# Define the structure for LASTINPUTINFO
$StructLASTINPUTINFO = @"
using System;
using System.Runtime.InteropServices;
public struct LASTINPUTINFO {
public uint cbSize;
public uint dwTime;
}
public class UserInput {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
public static uint GetIdleTime() {
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
GetLastInputInfo(ref lii);
return (uint)Environment.TickCount - lii.dwTime;
}
}
"@
# Add the type and call the GetIdleTime function
Add-Type $StructLASTINPUTINFO
$userIdleTime = [UserInput]::GetIdleTime()
# Example usage
Write-Host "User idle time: $($userIdleTime / 1000) seconds"
Comments
Post a Comment