commandline ผู้ใช้ของ Windows ออกจากระบบ


2

ฉันจะออกจากระบบผู้ใช้โดยใช้แม่มดบรรทัดคำสั่งควรจะทำโดยใช้ชื่อผู้ใช้ไม่ได้เซสชั่น id มันควรจะทำอย่างแข็งขัน

ฉันพยายาม: shutdown -lแต่มันใช้งานได้เฉพาะกับผู้ใช้ปัจจุบันลองlogoffคำสั่ง แต่มันต้องมีชื่อเซสชั่น

แก้ไข: ฉันได้ลองใช้สคริปต์ออกจากระบบแล้ว แต่มันไม่ทำงาน .. มีใครสามารถแก้ไขได้หรือไม่

ไฟล์ชุด

@echo off
query session > logoff.txt
for /f "skip=2 tokens=2,3" %%i in (logoff.txt) DO if [%%i]==[%1] logoff %%j

LOGOFF.TXT

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
 services                                    0  Disc                        
 console                                     1  Conn                        
>rdp-tcp#0         Administrator             2  Active                      
                   hi                        3  Disc                        
                   h                         4  Disc                        
                   Abdou                     5  Disc                        
                   Abdou76                   6  Disc                        
 rdp-tcp                                 65536  Listen                      

พยายามสคริปต์ .. แต่ล้มเหลว

C:\dir>logoff.bat Abdou76

C:\dir>

ผู้ใช้ Abdou76 ยังคงลงชื่อเข้าใช้


1
คุณใช้ Windows รุ่นใด ผู้ใช้รายอื่นเข้าสู่ระบบได้อย่างไร?
bosco

Windows Server 2012
Abdelhafidh Belalia

อ่ามันไม่ทำงานเพราะสำหรับผู้ใช้ที่ไม่ได้เชื่อมต่อโทเค็นแรก (คอนโซล, rdp-tcp # 0, ฯลฯ ) ไม่มีอยู่ ... นี่อาจจะเกินความสามารถของสคริปต์เล็กน้อย สคริปต์ PowerShell ตกลงหรือไม่
Ƭᴇcʜιᴇ007

@ Ƭᴇcʜιᴇ007ใช่ฉันเห็นไม่มี prolem โดยใช้สคริปต์ ps หรืออื่น ๆ
Abdelhafidh Belalia

คำตอบ:


2

Batch:

ไม่มียูทิลิตี้ "ออกจากระบบตามชื่อ" ของ Windows ที่ฉันทราบ

คุณสามารถใช้แบตช์ไฟล์เพื่อค้นหาเซสชันปัจจุบันเลือกชื่อและรหัสเซสชันจากนั้นออกจากระบบที่ตรงกับผู้ใช้:

@echo off
query session > sessioninfo.txt
for /f "skip=2 tokens=2,3" %%i in (sessioninfo.txt) DO if [%%i]==[%1] logoff %%j
del sessioninfo.txt

การใช้งาน:

batchfile.bat username

ชุดโหลดผลลัพธ์ของquery sessionการเป็นไฟล์ข้อความ

จากนั้นก็ใช้forห่วงข้ามสองบรรทัดแรกในการโหลดชื่อผู้ใช้และรหัสเซสชั่นราชสกุลสำหรับแต่ละสายเข้าและIJ

Iถูกตรวจสอบเพื่อดูว่ามันตรงกับชื่อที่ให้ไว้เป็นอาร์กิวเมนต์ (แสดงโดย%1) และถ้าlogoffเป็นเช่นนั้นจะใช้กับ ID เซสชั่นที่สอดคล้องกัน


หลังจากความคิดเห็นและรายละเอียดบางอย่างเกี่ยวกับผู้ใช้ "ไม่ได้เชื่อมต่อ" ฉันคิดว่าสิ่งนี้จะทำได้ดีกว่าใน PowerShell

PowerShell:

นี่คือสคริปต์ PowerShell ที่จะออกจากระบบเซสชันทั้งหมดที่ตรงกับชื่อผู้ใช้ที่คุณป้อนและจะทำงานหากผู้ใช้หากใช้งานอยู่หรือถูกตัดการเชื่อมต่อ

param (
    [string]$username = $(throw "-username is required.")
)

function Get-Sessions
{
    $queryResults = query session
    $starters = New-Object psobject -Property @{"SessionName" = 0; "UserName" = 0; "ID" = 0; "State" = 0; "Type" = 0; "Device" = 0;}
    foreach ($result in $queryResults)
    {
        try
        {
            if($result.trim().substring(0, $result.trim().indexof(" ")) -eq "SESSIONNAME") {
                $starters.UserName = $result.indexof("USERNAME");
                $starters.ID = $result.indexof("ID");
                $starters.State = $result.indexof("STATE");
                $starters.Type = $result.indexof("TYPE");
                $starters.Device = $result.indexof("DEVICE");
                continue;
            }

            New-Object psobject -Property @{
                "SessionName" = $result.trim().substring(0, $result.trim().indexof(" ")).trim(">");
                "Username" = $result.Substring($starters.Username, $result.IndexOf(" ", $starters.Username) - $starters.Username);
                "ID" = $result.Substring($result.IndexOf(" ", $starters.Username), $starters.ID - $result.IndexOf(" ", $starters.Username) + 2).trim();
                "State" = $result.Substring($starters.State, $result.IndexOf(" ", $starters.State)-$starters.State).trim();
                "Type" = $result.Substring($starters.Type, $starters.Device - $starters.Type).trim();
                "Device" = $result.Substring($starters.Device).trim()
            }
        } 
        catch 
        {
            $e = $_;
            throw "ERROR: " + $e.PSMessageDetails
        }
    }
}

$username = $username.ToLower()
$userSessions = Get-Sessions | ? { ($_.UserName).ToLower() -eq $username } | Select ID, UserName
$numberOfSessions = ($userSessions | measure).Count

if ($numberOfSessions -gt 0) {
    foreach ($session in $userSessions)
    {
       $sessionInfo = $session.Username + " (" + $session.ID + ")"
       Write-Host "Found $sessionInfo"
       logoff $session.ID /V
    }
} else {
    Write-Host """$username"" not found in session list."
}

การใช้งาน (จากภายใน PowerShell): .\LogEmOff.ps1 Abdou76

อาร์กิวเมนต์ชื่อผู้ใช้ไม่คำนึงถึงขนาดตัวพิมพ์ (เช่น: AbDoU76 = Abdou76 = ABDOU76 = abdou76)


ฉันลองผู้ดูแลระบบ logoff.bat แต่ฉันได้ "ถ้า [65536] == [ผู้ดูแลระบบ] ออกจากระบบฟัง"
Abdelhafidh Belalia

สคริปต์ทำงานได้ไม่ดีคำแนะนำใด ๆ
Abdelhafidh Belalia

หากคุณเห็น "ถ้า [65536] == [ผู้ดูแลระบบ] ออกจากระบบฟัง" เป็นบรรทัดสุดท้ายของเอาต์พุตนั่นหมายความว่าไม่พบผู้ใช้ "ผู้ดูแลระบบ" ในรายการ โปรดทราบว่ามันเป็นกรณี ๆ ไป โปรดแก้ไขคำถามของคุณและรวมเอาท์พุทของquery sessionและรวมถึงคำสั่ง (ชุด + ข้อโต้แย้ง) ที่คุณวิ่ง
Ƭᴇcʜιᴇ007

ฉันได้แก้ไขคำถามโปรดตรวจสอบปัญหาขอบคุณ
Abdelhafidh Belalia

ขอบคุณสำหรับความช่วยเหลือของคุณฉันได้โพสต์โซลูชันอื่นที่ตรงกับความต้องการของฉัน
Abdelhafidh Belalia

1

ฉันได้ค้นพบวิธีแก้ปัญหา: ใช้ชุดรหัส

@echo off
query user > logoff.txt
for /f "tokens=1,2" %%i in (logoff.txt) DO if /I [%%i]==[%1] logoff.exe %%j

การใช้งาน:
ชื่อผู้ใช้ออกจากระบบ

หมายเหตุ:อาร์กิวเมนต์ชื่อผู้ใช้ไม่คำนึงถึงขนาดตัวพิมพ์ (เช่น: ชื่อผู้ใช้ = ชื่อผู้ใช้ = USERNAME = ชื่อผู้ใช้)

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.