I want to detect whether or not a user has been active in the past 10 minutes using a batch file or some background process

时光总嘲笑我的痴心妄想 提交于 2019-12-23 06:14:12

问题


I've never worked with batch files before, but the idea behind this is that I want to simply run a program after I've been inactive for a certain period of time.
It doesn't matter which program, I just want it to run concurrently with my screensaver for some lighting effects with my keyboard.
I suppose another solution would be to detect whether the screensaver is running or not and start on that condition as well, I just have no idea where to begin with this one, but am willing to learn.


回答1:


This powershell gets all users last long on time. Change the time variable, should work.

Import-Module ActiveDirectory

function Get-ADUsersLastLogon()
{
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $users = Get-ADUser -Filter *
  $time = 0
  $exportFilePath = "c:\lastLogon.csv"
  $columns = "name,username,datetime"

  Out-File -filepath $exportFilePath -force -InputObject $columns

  foreach($user in $users)
  {
    foreach($dc in $dcs)
    { 
      $hostname = $dc.HostName
      $currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties     lastLogon

      if($currentUser.LastLogon -gt $time) 
      {
        $time = $currentUser.LastLogon
      }
    }

    $dt = [DateTime]::FromFileTime($time)
    $row = $user.Name+","+$user.SamAccountName+","+$dt

    Out-File -filepath $exportFilePath -append -noclobber -InputObject $row

    $time = 0
  }
}

Get-ADUsersLastLogon



回答2:


Just schedule a task in Task Scheduler to run only if user not used computer in last 10 mins.



来源:https://stackoverflow.com/questions/26029976/i-want-to-detect-whether-or-not-a-user-has-been-active-in-the-past-10-minutes-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!