Powershell 获取机器的空闲时间

Powershell 获取机器的空闲时间,powershell,Powershell,有没有办法使用Powershell或批处理文件以分钟/小时为单位获取机器的空闲时间,如机器未使用的时间?下面是一个使用Win32 API的Powershell解决方案 以及一个示例用法: for ( $i = 0; $i -lt 10; $i++ ) { Write-Host ("Last input " + [PInvoke.Win32.UserInput]::LastInput) Write-Host ("Idle for " + [PInvoke.Win32.UserInp

有没有办法使用Powershell或批处理文件以分钟/小时为单位获取机器的空闲时间,如机器未使用的时间?

下面是一个使用Win32 API的Powershell解决方案

以及一个示例用法:

for ( $i = 0; $i -lt 10; $i++ ) {
    Write-Host ("Last input " + [PInvoke.Win32.UserInput]::LastInput)
    Write-Host ("Idle for " + [PInvoke.Win32.UserInput]::IdleTime)
    Start-Sleep -Seconds (Get-Random -Minimum 1 -Maximum 5)
}

尝试此操作-虽然远程运行,但它可能在不同的会话上运行,因此空闲时间也不同?

此vbscript代码的灵感来自于回答的问题,向您展示如何在空闲超时后使用它自动关闭工作站

自动关机\u开启\u空闲\u超时。vbs



更准确地定义“未用于”。自从注销后,是否与中相同?屏幕保护程序运行了多长时间?到底是什么?我在这个网站上似乎什么都做不好。。。碰巧,下面的答案正是我想要的——从用户停止移动鼠标或按下键盘上的按钮开始的空闲时间。不确定为什么有人投票否决了你,可能问题有点模糊,或者没有显示你是否尝试过任何东西。。。一定要退房,非常感谢!正是我要找的。如何格式化输出,使其更具可读性,即4小时15分钟21秒?再次感谢!下面是一个很好的实现,用于在空闲时间后锁定计算机:这在远程机器上似乎不起作用;我认为这只会给当前用户会话留出空闲时间。伙计,这太棒了……要想让它在远程机器上工作,可以从一个计划任务中运行它,该任务以当前登录用户的身份运行代码(谷歌如何做到这一点)
for ( $i = 0; $i -lt 10; $i++ ) {
    Write-Host ("Last input " + [PInvoke.Win32.UserInput]::LastInput)
    Write-Host ("Idle for " + [PInvoke.Win32.UserInput]::IdleTime)
    Start-Sleep -Seconds (Get-Random -Minimum 1 -Maximum 5)
}
$Last = [PInvoke.Win32.UserInput]::LastInput
$Idle = [PInvoke.Win32.UserInput]::IdleTime
$LastStr = $Last.ToLocalTime().ToString("MM/dd/yyyy hh:mm tt")
Write-Host ("Last user keyboard/mouse input: " + $LastStr)
Write-Host ("Idle for " + $Idle.Days + " days, " + $Idle.Hours + " hours, " + $Idle.Minutes + " minutes, " + $Idle.Seconds + " seconds.")
'##########################################################################################################################
'# Auto-Shutdown your workstation after Idle Timeout                                                                      #
'# Script Name : Auto-Shutdown_On_Idle_TimeOut.vbs                                                                        #
'# Arrêt automatique de votre poste de travail après le délai d'inactivité                                                #
'# Idea comes from here ==>  This snippet is from http://stackoverflow.com/a/15846912                                     #
'# https://stackoverflow.com/questions/15845508/get-idle-time-of-machine/15846912#15846912                                #
'# https://gist.github.com/wendelb/1c364bb1a36ca5916ca4 ===> Auto-Lock your workstation after Idle-Timeout with PowerShell#
'##########################################################################################################################
Option Explicit
Dim Copyright,Msg,MsgEN,MsgFR
Copyright = "Auto-Shutdown your workstation after Idle Timeout " & ChrW(169) &" Hackoo 2020"

MsgEN = Array("ATTENTION ! There is another instance running !",_
"Save your Work because the computer will shutdown in 60 seconds")

MsgFR = Array("ATTENTION ! Il y a une autre instance en cours d'exécution !",_
"Sauvegarder votre Travail car l'ordinateur va s'éteindre dans 60 secondes")

If AppPrevInstance() Then 
    If Oslang = 1036 Then
        Msg = MsgFR(0) 
    Else
        Msg = MsgEN(0)
    End If
    MsgBox Msg & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,Copyright
    WScript.Quit   
Else 
    Dim Timeout_Idle,strCommand,VbsPath,ShortcutName
    If Oslang = 1036 Then
        Msg = MsgFR(1) 
    Else
        Msg = MsgEN(1)
    End If
    Timeout_Idle = "60"  '60 Minutes = 1 Heure = 1 Hour
    strCommand = "Shutdown.exe -s -t 60 -c " & DblQuote(Msg)
    VbsPath = Wscript.ScriptFullName
    ShortcutName = "Auto-Shutdown_On_Idle_TimeOut"
    Call Shortcut(VbsPath,ShortcutName)
    Call Write_Run_PScript(Timeout_Idle,strCommand)
End If
'---------------------------------------------------------------------------------------------------------------
Sub Shortcut(PathApplication,ShortcutName)
    Dim objShell,StartFolder,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If ShortcutName = "" Then
        ShortcutName = MyTab(UBound(MyTab))
    End if
    StartFolder = objShell.SpecialFolders("Startup")
    Set objShortCut = objShell.CreateShortcut(StartFolder & "\" & ShortcutName & ".lnk")
    objShortCut.TargetPath = DblQuote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,27"
    objShortCut.Save
End Sub
'---------------------------------------------------------------------------------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'---------------------------------------------------------------------------------------------------------------
Sub Write_Run_PScript(Timeout_Idle,strCommand)
    Const ForWriting = 2
    Dim fs,Ws,ts,Ret,PSFile,ByPassPSFile
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set Ws = CreateObject("WScript.Shell")
    PSFile = Ws.ExpandEnvironmentStrings("%Temp%") & fs.GetTempName & ".ps1"
    ByPassPSFile = "PowerShell -ExecutionPolicy bypass -noprofile -file "
    Set ts = fs.OpenTextFile(PSFile,ForWriting,True)
    ts.WriteLine "$idle_timeout = New-TimeSpan -Minutes "& Timeout_Idle &""
    ts.WriteLine "Add-Type @'"
    ts.WriteLine "using System;"
    ts.WriteLine "using System.Diagnostics;"
    ts.WriteLine "using System.Runtime.InteropServices;"
    ts.WriteLine "namespace PInvoke.Win32 {"
    ts.WriteLine "    public static class UserInput {"
    ts.WriteLine "        [DllImport(""user32.dll"", SetLastError=false)]"
    ts.WriteLine "        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);"
    ts.WriteLine "        [StructLayout(LayoutKind.Sequential)]"
    ts.WriteLine "        private struct LASTINPUTINFO {"
    ts.WriteLine "            public uint cbSize;"
    ts.WriteLine "            public int dwTime;"
    ts.WriteLine "        }"
    ts.WriteLine "        public static DateTime LastInput {"
    ts.WriteLine "            get {"
    ts.WriteLine "                DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);"
    ts.WriteLine "                DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);"
    ts.WriteLine "                return lastInput;"
    ts.WriteLine "            }"
    ts.WriteLine "        }"
    ts.WriteLine "        public static TimeSpan IdleTime {"
    ts.WriteLine "            get {"
    ts.WriteLine "                return DateTime.UtcNow.Subtract(LastInput);"
    ts.WriteLine "            }"
    ts.WriteLine "        }"
    ts.WriteLine "        public static int LastInputTicks {"
    ts.WriteLine "            get {"
    ts.WriteLine "                LASTINPUTINFO lii = new LASTINPUTINFO();"
    ts.WriteLine "                lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));"
    ts.WriteLine "                GetLastInputInfo(ref lii);"
    ts.WriteLine "                return lii.dwTime;"
    ts.WriteLine "            }"
    ts.WriteLine "       }"
    ts.WriteLine "    }"
    ts.WriteLine "}"
    ts.WriteLine "'@"
    ts.WriteLine "$locked = 0;"
    ts.WriteLine "Do {"
    ts.WriteLine "  $idle_time = [PInvoke.Win32.UserInput]::IdleTime;"
    ts.WriteLine "  if (($locked -eq 0) -And ($idle_time -gt $idle_timeout)) {"
    ts.WriteLine "      "&strCommand&""
    ts.WriteLine "      $locked = 1;"
    ts.WriteLine "  }"
    ts.WriteLine "  if ($idle_time -lt $idle_timeout) {"
    ts.WriteLine "      $locked = 0;"
    ts.WriteLine "  }"
    ts.WriteLine "    Start-Sleep -Seconds 10"
    ts.WriteLine "}"
    ts.WriteLine "while (1 -eq 1)"
    ts.Close
    Ret = Ws.run(ByPassPSFile & PSFile,0,True)
End sub
'----------------------------------------------------------------------------------------------------------------
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function    
'----------------------------------------------------------------------------------------------------------------
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'----------------------------------------------------------------------------------------------------------------
Function OSLang()
    Dim dtmConvertedDate,strComputer,objWMIService,oss,os
    Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    For Each os in oss
        OSLang = os.OSLanguage
    Next
End Function
'----------------------------------------------------------------------------------------------------------------