从服务器上注销所有用户(一个用户除外)

时间:2019-07-05 15:46:24

标签: windows powershell

我有这个脚本可以从服务器注销所有断开连接的用户。问题在于其中之一需要断开连接,并且无法注销。

问题在于该脚本可以处理query user的字符串结果,而且我不知道如何隔离其中一个用户。

$server = "localhost";

# Get all RDP sessions
$sessions = query user /server:$server | select -Skip 1;

# Loop through each session/line returned
foreach ($line in $sessions) {
    $line = -split $line;

    # Check for missing SessionName field/column
    if ($line.length -eq 8) {
        # Get current session state (column 4)
        $state = $line[3];

        # Get Session ID (column 3) and current idle time (column 5)
        $sessionid = $line[2];
        $idletime = $line[4];
    } else {
        # Get current session state (column 3)
        $state = $line[2];

        # Get Session ID (column 2) and current idle time (column 4)
        $sessionid = $line[1];
        $idletime = $line[3];
    }

    # If the session state is Disconnected
    if ($state -eq "Disc") {
        # Check if idle for more than 1 day (has a '+') and log off
        if ($idletime -like "*+*") {
            logoff $sessionid /server:$server /v
        } elseif ($idletime -like "*:*") {
            logoff $sessionid /server:$server /v
        }
    }
}

这是query user的示例结果。我想断开连接的用户是p.vbr.1

 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 a2270725-3                               13  Disc      2+00:17  7/2/2019 1:50 PM
 a2232655-3                               14  Disc      4+09:54  7/1/2019 2:10 AM
 a2129521-3                               30  Disc      2+04:50  7/1/2019 4:52 AM
 a16991754-3                              49  Disc        22:51  7/1/2019 5:44 AM
 p.vbr.1                                  58  Disc      4+20:19  6/25/2019 11:20 AM
 a16990384-3                              59  Disc         1:43  6/27/2019 10:20 AM
 a2169135-3                               68  Disc      3+00:50  7/2/2019 11:13 AM
 a2289685-3                               79  Disc         6:40  7/2/2019 9:04 PM
>a2310806-3            rdp-tcp#93         85  Active          .  7/1/2019 9:05 AM
 a16991667-3                              98  Disc      3+00:31  6/26/2019 6:35 AM
 a2064837-3                              107  Disc         8:32  7/3/2019 12:47 AM
 a2282463-3                              108  Disc      2+01:51  7/3/2019 8:55 AM
 a2292833-3                              116  Disc      1+21:30  7/3/2019 2:06 PM
 a18005447-3                             126  Disc      8+20:09  6/26/2019 2:48 PM
 a2185113-3                              135  Disc         9:19  6/26/2019 9:14 PM
 a2067993-3                              139  Disc      1+03:58  7/4/2019 8:08 AM
 a2101008-3                              140  Disc         5:10  7/3/2019 10:00 PM
 a2256517-3                              141  Disc      1+03:32  7/4/2019 8:32 AM
 a2340150-3                              142  Disc        12:35  7/4/2019 9:53 PM
 a2076309-3                              143  Disc         3:37  7/5/2019 3:37 AM

2 个答案:

答案 0 :(得分:1)

用户名似乎以$line[0]结尾,因此只需在循环开始时添加它即可:

if ($line[0] -eq "p.vbr.1") {continue;}

答案 1 :(得分:0)

考虑使用此ConvertFrom-SourceTable cmdlet。
它从固定宽度的表中创建公共对象,这使您的脚本更易于阅读和管理:

$Query = query user /server:$server
$Sessions = ConvertFrom-SourceTable -Literal $Query

$Sessions | Where-Object {$_.State -eq 'Disc'} | ForEach-Object {
    If ($_.UserName -eq "p.vbr.1") {Continue}
    If ($_.'Idle Time' -like "*+*") {
        logoff $_.Id /server:$server /v
    } ElseIf ($_.'Idle Time' -like "*:*") {
        logoff $_.Id /server:$server /v
    }
}