我试图运行https://stackoverflow.com/users/251123/andy-arismendi中发布的代码Get idle time of machine。它返回结果,就像我在本地运行时所期望的那样,但当我尝试在远程计算机上执行时,我得到了奇怪的结果:它告诉我超过13个小时的空闲时间 - 但我只是走出了这个用户&# 39;办公室,并知道空闲时间应该在几秒钟之内。
这是我正在运行的确切代码 - 从原始代码稍微调整一下:
# via https://stackoverflow.com/questions/15845508/get-idle-time-of-machine
# c:\admin\pstools\psexec.exe \\COMPUTERNAME powershell.exe -command Enable-PSRemoting
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;
}
}
}
}
'@
for ( $i = 0; $i -lt 10; $i++ ) {
$Last = [PInvoke.Win32.UserInput]::LastInput
$Idle = [PInvoke.Win32.UserInput]::IdleTime
$LastStr = $Last.ToLocalTime().ToString("MM/dd/yyyy hh:mm tt")
Write-Host ("`nTest " + $i)
Write-Host (" Last user keyboard/mouse input: " + $LastStr)
Write-Host (" Idle for " + $Idle.Days + " days, " + $Idle.Hours + " hours, " + $Idle.Minutes + " minutes, " + $Idle.Seconds + " seconds.")
Start-Sleep -Seconds (Get-Random -Minimum 1 -Maximum 10)
}
我像这样远程运行它:
Invoke-Command -ComputerName COMPUTERNAME -FilePath \\path\to\scripts\TESTING-IdleTime.ps1
我运行命令的计算机和远程计算机都是Windows 10 Pro 1709.
示例输出:
测试0
最后一个用户键盘/鼠标输入:01/10/2018 11:50 PM
闲置0天,13小时16分13秒。
我刚刚在计算机上尝试过,我知道当前没有人登录,并且它报告了同一时间 - 自上次重启系统以来的大致时间。
知道可能会发生什么吗?