PS to logout user from multiple servers

Anonymous
2022-04-15T00:13:07.71+00:00

I need help with a script that will logout Mr. Joe DiMaggio from a list of servers (+80 and growing) saved on my temp folder. Is it possible?

$servers = get-content -path c:\temp\server.txt
$userName = 'JDiMaggio'
$sessionId = ((quser /server:$servers | Where-Object { $_ -match $userName }) -split ' +')[2]
$sessionId
logoff $sessionId /server:$servers

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,454 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,431 Reputation points
    2022-04-15T02:24:15.43+00:00

    I haven't run this, but I think it'll work:

    $NameToZap = 'JDiMaggio'
    
    $USERNAME = 0
    $SESSIONNAME = 1
    $ID = 2
    
    Get-Content -path c:\temp\server.txt |
        ForEach-Object{
            $x = quser /server:$_
            for ($i = 1; $i -lt $x.count; $i++){    # skip the header
                $y = $x[$i].split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[$USERNAME..$ID]  # get the 1st 3 columns of data
                if ($y[$USERNAME] -eq $NameToZap){
                    logoff $y[$ID] /server:$_           # log off the session id
                }
        }
    }
    

0 additional answers

Sort by: Most helpful