PowerShell - Check servers from list who is logged in
- 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
Comments