我想使用批处理文件或某些后台进程检测用户在过去10分钟内是否处于活动状态

时间:2021-03-30 01:23:06

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.

我之前从未使用过批处理文件,但这背后的想法是我想在我处于非活动状态一段时间之后运行一个程序。哪个程序无关紧要,我只想让它与我的屏幕保护程序同时运行,以获得我的键盘的一些照明效果。我想另一个解决方案是检测屏幕保护程序是否正在运行,并开始这种情况,我只是不知道从哪一个开始,但我愿意学习。

2 个解决方案

#1


0  

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

这个powershell可以让所有用户按时完成。更改时间变量,应该工作。

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


0  

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

只安排任务计划程序中的任务仅在用户最近10分钟内未使用计算机时运行。

#1


0  

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

这个powershell可以让所有用户按时完成。更改时间变量,应该工作。

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


0  

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

只安排任务计划程序中的任务仅在用户最近10分钟内未使用计算机时运行。