Powershell quser&注销

时间:2015-03-30 12:20:28

标签: powershell puppet

尝试创建一个脚本以注销已闲置超过10分钟的用户。 我的空闲时间-gt不起作用。

foreach ($user in quser | select ID, username, 'idle time' )   {

if($user.'IDLE TIME' -gt 10) {

    logoff /server:localhost  $user.ID
    $Output = "Logging off $user.username"}
}  

else {$Output = "No users to log off" }

} 

感谢您的帮助

这最终有效,但我已将脚本配置如下,但没有使用idletime,只列出了要从服务器注销的用户名。以下是通过木偶运行所以它只在早上2 - 5之间执行。

$localServerName = Get-WmiObject -Class Win32_ComputerSystem | Select Name
$servername = $localServerName.Name
$Jallie =@()
foreach($users in Get-WmiObject win32_process -ComputerName $localServerName.Name | where {$_.ProcessName -eq 'explorer.exe'} )
{
     $temp = " " | select Processname,Username,SessionId
     $temp.Processname = $users.ProcessName
     $temp.Username = $users.GetOwner().user
     $temp.Sessionid = $users.SessionId
     $Jallie += $temp
}
$servername = $localServerName.Name
foreach($element in $Jallie)
{
      if($element.username -eq 'user1')
      {
            logoff $element.Sessionid /SERVER:$servername
            'Logging off ' + $element.username
      }
      elseif($element.username -eq 'user2')
      {
            logoff $element.Sessionid /SERVER:$servername
            'Logging off ' + $element.username
      }

}

0 个答案:

没有答案