top of page

PowerShell - Check servers from list who is logged in

  • Writer: Jon Boyette
    Jon Boyette
  • Jan 10, 2022
  • 1 min read

Save this as a name.ps1 and add your Domain.Com, server list is populated at c:\temp\servers.txt

$ServerList = Get-Content "c:\temp\servers.txt"

$ComputerName = $ServerList

$Domain = "YOURDOMAIN.COM"

$SessionToLogOff = Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-Process -IncludeUserName | Select-Object UserName,SessionId | Where-Object {$PSItem.UserName -ne $null -and $PSItem.UserName.StartsWith($Domain) -and $PSItem.SessionId -ne "0"}} | sort sessionID -Unique | ogv -PassThru


if ($SessionToLogOff) {

#Invoke-RDUserLogoff -HostServer $ComputerName -UnifiedSessionID (Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-Process -IncludeUserName | Select-Object UserName,SessionId | Where-Object {$PSItem.UserName -ne $null -and $PSItem.UserName.StartsWith($Domain) -and $PSItem.SessionId -ne "0"} -ErrorAction SilentlyContinue | Sort-Object SessionId -Unique} | Select-Object UserName,SessionId | OGV -PassThru).SessionId -Force

Invoke-RDUserLogoff -HostServer $ComputerName -UnifiedSessionID $SessionToLogOff.SessionId -ErrorAction SilentlyContinue -Force

} #end if

else {

Write-host "No users logged on $ComputerName" -ForegroundColor Green

} #end else



 
 
 

Recent Posts

See All
PowerShell - List All Domain SPNs

Save as same List_ALL_SPNs.ps1 or similar, this LDap calls the Domain for all Service Principal names and accounts related #Build LDAP...

 
 
 
PowerShell - Start-Monitoring

This is a great script used to Monitor and Email if a server is up or down, once ran, and smtp and from address is set, then run:...

 
 
 

Comments


Post: Blog2 Post
  • Facebook
  • Twitter
  • LinkedIn

©2022 by Boyette Technical Services. Proudly created with Wix.com

bottom of page