powershell - Get idle time of machine -
is there way idle time of machine, in amount of time machine has not been used for, in minutes/hours using powershell or batch file?
here's powershell solution uses win32 api getlastinputinfo.
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 { { datetime boottime = datetime.utcnow.addmilliseconds(-environment.tickcount); datetime lastinput = boottime.addmilliseconds(lastinputticks); return lastinput; } } public static timespan idletime { { return datetime.utcnow.subtract(lastinput); } } public static int lastinputticks { { lastinputinfo lii = new lastinputinfo(); lii.cbsize = (uint)marshal.sizeof(typeof(lastinputinfo)); getlastinputinfo(ref lii); return lii.dwtime; } } } } '@ and example usage:
for ( $i = 0; $i -lt 10; $i++ ) { write-host ("last input " + [pinvoke.win32.userinput]::lastinput) write-host ("idle " + [pinvoke.win32.userinput]::idletime) start-sleep -seconds (get-random -minimum 1 -maximum 5) }
Comments
Post a Comment