我正在尝试编写.bat脚本,该脚本在从远程桌面闲置24小时后将注销用户。该脚本可以正常运行到59分钟,但无法在数小时内执行相同的操作。无法进行转换。 5分钟后,它将在此处注销空闲用户。
@echo off
:Top
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 5 (logoff %%b) else %%e GTR 5 (logoff %%b)
choice /T 120 /C 1 /D 1 /N
goto top
我也想介绍这种情况,因为此批处理脚本将从任务计划程序每四个小时运行一次。
答案 0 :(得分:0)
请尝试一下,我仅将%%e
分配给val
,因为那是我系统上的令牌位置,因此,如果您的空闲时间在该时间,请随时设置val=%%d
时间。我不能测试这个,因为我没有相同的环境。
@echo off
:Top
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do set "val=%%e"
for /f "tokens=1,* delims=:" %%i in ("%val%") do if %%i GEQ 23 if %%j GTR 58 logoff %%b
choice /T 120 /C 1 /D 1 /N
goto top
答案 1 :(得分:0)
以下是执行此任务的Powershell脚本:
if($server -eq $null){
$server = "localhost"
}
$users = @()
# Query using quser, 2>$null to hide "No users exists...", then skip to the next server
$quser = quser /server:$server 2>$null
if(!($quser)){
Continue
}
#Remove column headers
$quser = $quser[1..$($quser.Count)]
foreach($user in $quser){
$usersObj = [PSCustomObject]@{Server=$null;Username=$null;SessionName=$null;SessionId=$Null;SessionState=$null;LogonTime=$null;IdleTime=$null}
$quserData = $user -split "\s+"
#We have to splice the array if the session is disconnected (as the SESSIONNAME column quserData[2] is empty)
if(($user | select-string "Disc") -ne $null){
#User is disconnected
$quserData = ($quserData[0..1],"null",$quserData[2..($quserData.Length -1)]) -split "\s+"
}
# Server
$usersObj.Server = $server
# Username
$usersObj.Username = $quserData[1]
# SessionName
$usersObj.SessionName = $quserData[2]
# SessionID
$usersObj.SessionID = $quserData[3]
# SessionState
$usersObj.SessionState = $quserData[4]
# IdleTime
$quserData[5] = $quserData[5] -replace "\+",":" -replace "\.","0:0" -replace "Disc","0:0"
if($quserData[5] -like "*:*"){
$usersObj.IdleTime = [timespan]"$($quserData[5])"
}elseif($quserData[5] -eq "." -or $quserData[5] -eq "none"){
$usersObj.idleTime = [timespan]"0:0"
}else{
$usersObj.IdleTime = [timespan]"0:$($quserData[5])"
}
# LogonTime
$usersObj.LogonTime = (Get-Date "$($quserData[6]) $($quserData[7]) $($quserData[8] )")
$users += $usersObj
if($usersObj.idleTime -ge [timespan]"23:58" -and $usersObj.SessionState -eq "Disc")
{
Logoff $quserData[3]
}
}
return $users
}
$quer= Get-LoggedOnUsers````