-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGet-DeviceIdleTIme.ps1
62 lines (49 loc) · 1.92 KB
/
Get-DeviceIdleTIme.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Import-Module $env:SyncroModule
# Credit for the PInvoke code to Andy Arismendi
# (see https://stackoverflow.com/questions/15845508/get-idle-time-of-machine)
# Adaptation for Syncro by Bill Bardon
#NOTE: Add a custom asset field named "Idle time", or choose your own name and modify the last line of this script to match
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;
}
}
}
}
'@
$Last = [PInvoke.Win32.UserInput]::LastInput
$Idle = [PInvoke.Win32.UserInput]::IdleTime
$DTnow = [DateTimeOffset]::Now
$LastStr = $Last.ToLocalTime().ToString("MMM d h:mm tt")
$IdleStr = $Idle.ToString("d\d\ h\h\ m\m")
$DTnowStr = $DTnow.ToString("MMM d h:mm tt")
Write-Host "$DTnowStr, last input was at $LastStr, Idle for $IdleStr"
Set-Asset-Field -Subdomain $subdomain -Name "Idle time" -Value "Last input was at $LastStr, Idle for $IdleStr"